国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁(yè) > 語(yǔ)言 > PHP > 正文

php單鏈表實(shí)現(xiàn)代碼分享

2024-05-04 23:47:38
字體:
來(lái)源:轉(zhuǎn)載
供稿:網(wǎng)友

本文實(shí)例為大家分享了php單鏈表的具體代碼,供大家參考,具體內(nèi)容如下

<?php/** * 單鏈表 */ class Demo{  private $id;  public $name;  public $next;  public function __construct ($id = '', $name = '')  {    $this->id = $id;    $this->name = $name;  }  static public function show ($head)  {    $cur = $head;    while ($cur->next) {      echo $cur->next->id,'###',$cur->next->name,'<br />';      $cur = $cur->next;    }    echo '<hr />';  }  //尾插法  static public function push ($head, $node)  {    $cur = $head;    while (NULL != $cur->next) {      $cur = $cur->next;    }    $cur->next = $node;    return $head;  }  static public function insert($head, $node)  {    $cur = $head;    while (NULL != $cur->next) {      if ($cur->next->id > $node->id) {        break;      }      $cur = $cur->next;    }    $node->next = $cur->next;    $cur->next = $node;    return $head;  }  static public function edit($head, $node)  {    $cur = $head;    while (NULL != $cur->next) {      if ($cur->next->id == $node->id) {        break;      }      $cur = $cur->next;    }    $cur->next->name = $node->name;    return $head;      }  static public function pop ($head, $node)  {    $cur = $head;    while (NULL != $cur->next) {      if ($cur->next == $node) {        break;      }      $cur = $cur->next;    }    $cur->next = $node->next;    return $head;        }}$team = new Demo();$node1 = new Demo(1, '唐三藏');Demo::push($team, $node1);$node1->name = '唐僧';Demo::show($team);// Demo::show($team);$node2 = new Demo(2, '孫悟空');Demo::insert($team, $node2);// Demo::show($team);$node3 = new Demo(5, '白龍馬');Demo::push($team, $node3);// Demo::show($team);$node4 = new Demo(3, '豬八戒');Demo::insert($team, $node4);// Demo::show($team);$node5 = new Demo(4, '沙和尚');Demo::insert($team, $node5);// Demo::show($team);$node4->name = '豬悟能';//php對(duì)象傳引用,所以Demo::edit沒(méi)有必要// unset($node4);// $node4 = new Demo(3, '豬悟能');// Demo::edit($team, $node4);Demo::pop($team, $node1);Demo::show($team);

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家實(shí)現(xiàn)php單鏈表有所幫助


注:相關(guān)教程知識(shí)閱讀請(qǐng)移步到PHP教程頻道。
發(fā)表評(píng)論 共有條評(píng)論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表

圖片精選

主站蜘蛛池模板: 舒城县| 恩平市| 宁陵县| 梓潼县| 高邑县| 元谋县| 祥云县| 博乐市| 阜康市| 卢龙县| 杭锦后旗| 尚义县| 新田县| 宜君县| 微山县| 东乡县| 荆州市| 广宁县| 射洪县| 湘潭市| 赤峰市| 玛沁县| 景洪市| 连平县| 沙田区| 芷江| 芦溪县| 凯里市| 新平| 绍兴市| 大理市| 新龙县| 桃江县| 元阳县| 武定县| 烟台市| 柳河县| 永昌县| 松潘县| 夏河县| 光泽县|