最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

PHP中模擬鏈表和鏈表的基本操作示例

 更新時間:2016年02月27日 16:03:52   作者:常思過  
這篇文章主要介紹了PHP中模擬鏈表和列表的基本操作示例,包括鏈表的增刪查改等,說明基本上都包含在代碼注釋中,需要的朋友可以參考下

模擬鏈表:

<?php 
/**
 * PHP實現(xiàn)鏈表的基本操作
 */
class linkList {
  /**
   * 姓名
   * @var string
   */
  public $name = '';
   
  /**
   * 編號
   * @var int
   */
  public $id = 0;
   
  /*
   * 引用下一個對象
   */
  public $next = null;
   
  /**
   * 構(gòu)造函數(shù)初始化數(shù)據(jù)
   * @param int $id
   * @param string $name
   */
  public function __construct($id = 0, $name = '') {
    $this->name = $name;
    $this->id  = $id;
  }
   
  /**
   * 遍歷鏈表
   */
  public static function echo_link_list($head) {
    $curr = $head;
    while ($curr->next != null) {
      echo '姓名:'.$curr->next->name, ' 編號:'.$curr->next->id;
      echo '<br>';
      $curr = $curr->next;
    }
  }
   
  /**
   * 添加新節(jié)點
   */
  public static function add($head, $id, $name) {
    $curr = $head;
    $obj = new linkList($id, $name);
     
    while ($curr->next != null) {
      // 如果當前ID < 下一個ID,則添加到中間,添加節(jié)點到指定順序位置
      if ($curr->next->id > $id) {
         
        $obj->next = $curr->next;
        $curr->next = $obj;
         
        return true;
      } else if ($curr->next->id == $id) {
         
        echo '當前Id:'.$id.'重復了,請不要繼續(xù)添加了!';
        echo '<br>';
         
        return false;
      }
      $curr = $curr->next;
    }
    // 添加節(jié)點到尾部
    if ($curr->next == null) {
      $curr->next = $obj;
    }
  }
   
  /**
   * 刪除節(jié)點
   */
  public static function del($head, $id) {
    $curr = $head;
     
    while($curr->next != null) {
      if ($curr->next->id == $id) {
        $curr->next = $curr->next->next;
        return true;
      }
      $curr = $curr->next;
    }
  }
   
  /**
   * 修改節(jié)點
   */
  public static function edit($head, $id, $new_name) {
    $curr = $head;
     
    while($curr->next != null) {
      if ($curr->next->id == $id) {
        $curr->next->name = $new_name;
      }
      $curr = $curr->next;
    }
  }
}
 
$head = new linkList();
linkList::add($head, 1, 'wangdk');
linkList::add($head, 2, 'sunshuzhen');
linkList::add($head, 8, 'wanghaha');
linkList::add($head, 6, 'wangchufen');
linkList::add($head, 6, 'wangchufen');
linkList::add($head, 3, 'wangdaye');
 
linkList::del($head, 1);
linkList::edit($head, 2, 'hahaha');
linkList::echo_link_list($head);
 
?>

鏈表的增刪查改:

<?php 
/**
 * PHP實現(xiàn)鏈表的基本操作
 */
class linkList {
  /**
   * 姓名
   * @var string
   */
  public $name = '';
   
  /**
   * 編號
   * @var int
   */
  public $id = 0;
   
  /*
   * 引用下一個對象
   */
  public $next = null;
   
  /**
   * 構(gòu)造函數(shù)初始化數(shù)據(jù)
   * @param int $id
   * @param string $name
   */
  public function __construct($id = 0, $name = '') {
    $this->name = $name;
    $this->id  = $id;
  }
   
  /**
   * 遍歷鏈表
   */
  public static function echo_link_list($head) {
    $curr = $head;
    while ($curr->next != null) {
      echo '姓名:'.$curr->next->name, ' 編號:'.$curr->next->id;
      echo '<br>';
      $curr = $curr->next;
    }
  }
   
  /**
   * 添加新節(jié)點
   */
  public static function add($head, $id, $name) {
    $curr = $head;
    $obj = new linkList($id, $name);
     
    while ($curr->next != null) {
      // 如果當前ID < 下一個ID,則添加到中間,添加節(jié)點到指定順序位置
      if ($curr->next->id > $id) {
         
        $obj->next = $curr->next;
        $curr->next = $obj;
         
        return true;
      } else if ($curr->next->id == $id) {
         
        echo '當前Id:'.$id.'重復了,請不要繼續(xù)添加了!';
        echo '<br>';
         
        return false;
      }
      $curr = $curr->next;
    }
    // 添加節(jié)點到尾部
    if ($curr->next == null) {
      $curr->next = $obj;
    }
  }
   
  /**
   * 刪除節(jié)點
   */
  public static function del($head, $id) {
    $curr = $head;
     
    while($curr->next != null) {
      if ($curr->next->id == $id) {
        $curr->next = $curr->next->next;
        return true;
      }
      $curr = $curr->next;
    }
  }
   
  /**
   * 修改節(jié)點
   */
  public static function edit($head, $id, $new_name) {
    $curr = $head;
     
    while($curr->next != null) {
      if ($curr->next->id == $id) {
        $curr->next->name = $new_name;
      }
      $curr = $curr->next;
    }
  }
}
 
$head = new linkList();
linkList::add($head, 1, 'wangdk');
linkList::add($head, 2, 'sunshuzhen');
linkList::add($head, 8, 'wanghaha');
linkList::add($head, 6, 'wangchufen');
linkList::add($head, 6, 'wangchufen');
linkList::add($head, 3, 'wangdaye');
 
linkList::del($head, 1);
linkList::edit($head, 2, 'hahaha');
linkList::echo_link_list($head);
 
?>

相關文章

最新評論

响水县| 南充市| 濉溪县| 苗栗县| 佛冈县| 高陵县| 镇巴县| 丹寨县| 东山县| 石棉县| 西华县| 县级市| 河北区| 鄂伦春自治旗| 当雄县| 龙江县| 贵溪市| 金平| 忻州市| 凌海市| 乌苏市| 甘孜| 车险| 隆子县| 海原县| 德昌县| 宝清县| 遵义县| 客服| 安庆市| 雷波县| 紫云| 汤原县| 连州市| 康马县| 霍林郭勒市| 九龙县| 富平县| 芜湖市| 博湖县| 阿瓦提县|