php 实现双向链表

  1 /**
  2  * 链表元素结点类
  3  */
  4 class Node {
  5     public $pre = NULL; // 前驱
  6     public $next = NULL; // 后继
  7     public $data = NULL; // 结点值
  8     public function __Construct($data) {
  9         $this->data = $data;
 10     }
 11 }
 12 
 13 /**
 14  * 双向链表类
 15  */
 16 class DoubleLink {
 17     private $head; // 头指针
 18     private $tail; // 尾指针
 19     private $len; // 链表长度
 20 
 21     /**
 22      * 初始化链表
 23      */
 24     public function __Construct()
 25     {
 26         $newNode = $newNode = new Node(null);
 27         $this->tail = $this->head = $newNode;
 28         $this->len = 0;
 29     }
 30 
 31     /**
 32      * 添加结点
 33      * @param $data 要添加的结点
 34      * @param $search 添加的位置
 35      */
 36     public function addNode($data, $search = null) {
 37         $newNode = new Node($data);
 38         $tmp = $this->searchNode($search);
 39         if ($tmp !== null) {
 40             $newNode->pre = $tmp->pre;
 41             $newNode->next = $tmp;
 42             $tmp->pre = $newNode;
 43             $newNode->pre->next = $newNode;
 44         } else {
 45             $newNode->pre = $this->tail;
 46             $this->tail->next = $newNode;
 47             $this->tail = $newNode;
 48         }
 49         $this->len++;
 50     }
 51 
 52     /**
 53      * 删除指定结点
 54      * @param $search
 55      */
 56     public function delNode($search) {
 57         $tmp = $this->searchNode($search);
 58         if(null !== $tmp){
 59             if ($tmp->next !== null) {
 60                 $tmp->pre->next = $tmp->next;
 61                 $tmp->next->pre = $tmp->pre;
 62             } else {
 63                 $tmp->pre->next = null;
 64             }
 65             unset($tmp);
 66             $this->len--;
 67         }
 68     }
 69 
 70     /**
 71      * 修改指定结点的值
 72      * @param $search
 73      * @param $data
 74      */
 75     public function setNode($search, $data){
 76         $tmp = $this->searchNode($search);
 77         if(null !== $tmp){
 78             $tmp->data = $data;
 79         }
 80     }
 81 
 82     /**
 83      * 查找结点
 84      * @param $search 要查找的结点元素值
 85      * @return $tmp 查找到的结点元素
 86      */
 87     public function searchNode($search) {
 88         $tmp = $this->head;
 89         while ( $tmp->next !== null ) {
 90             $tmp = $tmp->next;
 91             if ($tmp->data === $search) {
 92                 return $tmp;
 93             }
 94         }
 95         return null;
 96     }
 97 
 98     /**
 99      * 读取链表全部结点
100      */
101     public function show() {
102         $tmp = $this->head;
103         while ( $tmp->next !== null ) {
104             $tmp = $tmp->next;
105             echo $tmp->data;
106         }
107         echo "<br/>";
108         $tmp = $this->tail;
109         while ( $tmp->pre !== null ) {
110             echo $tmp->data;
111             $tmp = $tmp->pre;
112         }
113     }
114 }
115 
116 $myList = new DoubleLink();
117 $myList->addNode("A");
118 $myList->addNode("B");
119 $myList->addNode("C");
120 $myList->addNode("D");
121 $myList->addNode("E");
122 $myList->addNode('F','C');
123 $myList->delNode('F');
124 $myList->setNode("B",'G');
125 $myList->show();

 

posted @ 2017-09-08 12:56  lpfuture  阅读(1178)  评论(1编辑  收藏  举报