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

首頁 > 語言 > PHP > 正文

PHP實現雙鏈表刪除與插入節點的方法示例

2024-05-05 00:01:01
字體:
來源:轉載
供稿:網友

本文實例講述了PHP實現雙鏈表刪除與插入節點的方法。分享給大家供大家參考,具體如下:

概述:

雙向鏈表也叫雙鏈表,是鏈表的一種,它的每個數據結點中都有兩個指針,分別指向直接后繼和直接前驅。所以,從雙向鏈表中的任意一個結點開始,都可以很方便地訪問它的前驅結點和后繼結點。一般我們都構造雙向循環鏈表。

實現代碼:

<?php class node{  public $prev;  public $next;  public $data;  public function __construct($data,$prev=null,$next=null){    $this->data=$data;    $this->prev=$prev;    $this->next=$next;  }}class doubleLinkList{  private $head;  public function __construct()  {    $this->head=new node("head",null,null);  }  //插入節點  public function insertLink($data){    $p=new node($data,null,null);    $q=$this->head->next;    $r=$this->head;    while($q){      if($q->data>$data){        $q->prev->next=$p;        $p->prev=$q->prev;        $p->next=$q;        $q->prev=$p;      }else{      $r=$q;$q=$q->next;      }    }    if($q==null){      $r->next=$p;      $p->prev=$r;    }  }  //從頭輸出節點  public function printFromFront(){    $p=$this->head->next;    $string="";    while($p){    $string.=$string?",":"";    $string.=$p->data;    $p=$p->next;    }    echo $string."<br>";  }  //從尾輸出節點  public function printFromEnd(){    $p=$this->head->next;    $r=$this->head;    while($p){    $r=$p;$p=$p->next;    }    $string="";    while($r){      $string.=$string?",":"";      $string.=$r->data;      $r=$r->prev;    }    echo $string."<br>";  }  public function delLink($data){    $p=$this->head->next;    if(!$p)    return;    while($p){      if($p->data==$data)      {        $p->next->prev=$p->prev;        $p->prev->next=$p->next;        unset($p);        return;      }      else{        $p=$p->next;      }    }    if($p==null)    echo "沒有值為{$data}的節點";  }}$link=new doubleLinkList();$link->insertLink(1);$link->insertLink(2);$link->insertLink(3);$link->insertLink(4);$link->insertLink(5);$link->delLink(3);$link->printFromFront();$link->printFromEnd();$link->delLink(6);

運行結果:

1,2,4,55,4,2,1,head沒有值為6的節點

希望本文所述對大家PHP程序設計有所幫助。


注:相關教程知識閱讀請移步到PHP教程頻道。
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表

圖片精選

主站蜘蛛池模板: 石楼县| 武陟县| 清水河县| 麻江县| 泾川县| 驻马店市| 桐柏县| 福建省| 郁南县| 鄂托克旗| 洞头县| 康乐县| 玛纳斯县| 阳泉市| 临猗县| 九龙城区| 江门市| 南京市| 锦州市| 江安县| 璧山县| 临海市| 河北省| 邵阳市| 巴林右旗| 武宁县| 花垣县| 陇川县| 娄烦县| 化德县| 西青区| 乐亭县| 金塔县| 阿瓦提县| 中宁县| 东源县| 浪卡子县| 三都| 大宁县| 怀仁县| 龙胜|