鏈表分爲單鏈表、雙鏈表、循環鏈表。
1、單鏈表
插入:鏈表中插入一個節點的效率很高。向鏈表中插入一個節點,須要修改它前面的節點(前驅),使其指向新加入的節點,而新加入的節點則指向原來前驅指向的節點(見下圖)。php
由上圖可知,B、C之間插入D,三者之間的關係爲html
current爲插入節點的前驅節點
current->next = new // B節點指向新節點D
new->next = current->next // 新節點D指向B的後繼節點C
刪除:從鏈表中刪除一個元素,將待刪除元素的前驅節點指向待刪除元素的後繼節點,同時將待刪除元素指向 null,元素就刪除成功了(見下圖)。ide
由上圖可知,A、C之間刪除B,三者之間的關係爲post
current爲要刪除節點的前驅節點
current->next = current->next->next // A節點指向C節點
具體代碼以下:測試


<?php // 節點類 class Node { public $data; // 節點數據 public $next; // 下一節點 public function __construct($data) { $this->data = $data; $this->next = NULL; } } // 單鏈表類 class SingleLinkedList { private $header; // 頭節點 function __construct($data) { $this->header = new Node($data); } // 查找節點 public function find($item) { $current = $this->header; while ($current->data != $item) { $current = $current->next; } return $current; } // (在節點後)插入新節點 public function insert($item, $new) { $newNode = new Node($new); $current = $this->find($item); $newNode->next = $current->next; $current->next = $newNode; return true; } // 更新節點 public function update($old, $new) { $current = $this->header; if ($current->next == null) { echo "鏈表爲空!"; return; } while ($current->next != null) { if ($current->data == $old) { break; } $current = $current->next; } return $current->data = $new; } // 查找待刪除節點的前一個節點 public function findPrevious($item) { $current = $this->header; while ($current->next != null && $current->next->data != $item) { $current = $current->next; } return $current; } // 從鏈表中刪除一個節點 public function delete($item) { $previous = $this->findPrevious($item); if ($previous->next != null) { $previous->next = $previous->next->next; } } // findPrevious和delete的整合 public function remove($item) { $current = $this->header; while ($current->next != null && $current->next->data != $item) { $current = $current->next; } if ($current->next != null) { $current->next = $current->next->next; } } // 清空鏈表 public function clear() { $this->header = null; } // 顯示鏈表中的元素 public function display() { $current = $this->header; if ($current->next == null) { echo "鏈表爲空!"; return; } while ($current->next != null) { echo $current->next->data . "  "; $current = $current->next; } } } $linkedList = new SingleLinkedList('header'); $linkedList->insert('header', 'China'); $linkedList->insert('China', 'USA'); $linkedList->insert('USA','England'); $linkedList->insert('England','Australia'); echo '鏈表爲:'; $linkedList->display(); echo "</br>"; echo '-----刪除節點USA-----'; echo "</br>"; $linkedList->delete('USA'); echo '鏈表爲:'; $linkedList->display(); echo "</br>"; echo '-----更新節點England爲Japan-----'; echo "</br>"; $linkedList->update('England', 'Japan'); echo '鏈表爲:'; $linkedList->display(); //echo "</br>"; //echo "-----清空鏈表-----"; //echo "</br>"; //$linkedList->clear(); //$linkedList->display(); // 輸出: 鏈表爲:China USA England Australia -----刪除節點USA----- 鏈表爲:China England Australia -----更新節點England爲Japan----- 鏈表爲:China Japan Australia
2、雙鏈表this
單鏈表從鏈表的頭節點遍歷到尾節點很簡單,但從後向前遍歷就沒那麼簡單了。雙鏈表的每一個數據結點中都有兩個指針,分別指向直接後繼和直接前驅。
因此,從雙向鏈表中的任意一個結點開始,均可以很方便地訪問它的前驅結點和後繼結點。

插入:插入一個節點時,須要指出該節點正確的前驅和後繼。
修改待插入節點的
前驅節點的next屬性,使其指向新加入的節點,而新插入的節點的next屬性則指向原來前驅指向的節點,同時將原來前驅指向的節點的previous屬性指向新節點,而新加入節點的previous屬性指向它前驅節點(見下圖)。

由上圖可知,B、C之間插入D,三者之間的關係爲
current爲插入節點的前驅節點
current->next = new // B的next屬性指向新節點D
new->next = current->next // 新節點D的next屬性指向B的後繼節點C
current->next->previous = new // B的後繼節點C的previous屬性指向新節點D(原先是C的previous屬性指向B)
刪除:雙向鏈表的刪除操做比單向鏈表的效率更高,由於不須要再查找前驅節點了。
首先須要在鏈表中找出存儲待刪除數據的節點,而後設置該節點前驅的 next 屬性,使其指向待刪除節點的後繼;設置該節點後繼的 previous 屬性,使其指向待刪除節點的前驅。

由上圖可知,B、C之間刪除D,三者之間的關係爲
current爲要刪除的節點
current->previous->next = current->next // B的前驅節點A的next屬性指向B的後繼節點C
current->next->previous = current->previous // B的後繼節點C的previous屬性指向B的前驅節點A
current->previous = null // B的previous屬性指向null
current->next = null // B的next屬性指向null
具體代碼以下:


<?php // 節點類 class Node { public $data; // 節點數據 public $previous = NULL; // 前驅 public $next = NULL; // 後繼 public function __construct($data) { $this->data = $data; $this->previous = NULL; $this->next = NULL; } } // 雙鏈表類 class DoubleLinkedList { private $header; // 頭節點 function __construct($data) { $this->header = new Node($data); } // 查找節點 public function find($item) { $current = $this->header; while ($current->data != $item) { $current = $current->next; } return $current; } // 查找鏈表最後一個節點 public function findLast() { $current = $this->header; while ($current->next != null) { $current = $current->next; } return $current; } //(在節點後)插入新節點 public function insert($item, $new) { $newNode = new Node($new); $current = $this->find($item); $newNode->next = $current->next; $newNode->previous = $current; $current->next = $newNode; return true; } // 從鏈表中刪除一個節點 public function delete($item) { $current = $this->find($item); if ($current->next != null) { $current->previous->next = $current->next; $current->next->previous = $current->previous; $current->next = null; $current->previous = null; return true; } } // 顯示鏈表中的元素 public function display() { $current = $this->header; if ($current->next == null) { echo "鏈表爲空!"; return; } while ($current->next != null) { echo $current->next->data . "  "; $current = $current->next; } } // 反序顯示雙向鏈表中的元素 public function dispReverse() { $current = $this->findLast(); while ($current->previous != null) { echo $current->data . "  "; $current = $current->previous; } } } // 測試 $linkedList = new DoubleLinkedList('header'); $linkedList->insert('header', 'China'); $linkedList->insert('China', 'USA'); $linkedList->insert('USA','England'); $linkedList->insert('England','Australia'); echo '鏈表爲:'; $linkedList->display(); echo "</br>"; echo '-----刪除節點USA-----'; echo "</br>"; $linkedList->delete('USA'); echo '鏈表爲:'; $linkedList->display(); // 輸出: 鏈表爲:China USA England Australia -----刪除節點USA----- 鏈表爲:China England Australia 複製代碼
3、循環鏈表spa
循環鏈表和單向鏈表類似,節點類型都是同樣的。惟一的區別是,在建立循環鏈表時,讓其頭節點的 next 屬性指向它自己,即:head.next = head,這種行爲會傳導至鏈表中的每一個節點,使得每一個節點的 next 屬性都指向鏈表的頭節點。換句話說,鏈表的尾節點指向頭節點,造成了一個循環鏈表。

在循環鏈表中,涉及遍歷的操做,其終止條件是判斷它們是否等於頭結點,而不是像單鏈表那樣判別p或p->next是否爲空。
插入:若是不是在鏈表尾端插入,則與單鏈表類似,將待插入節點的前驅節點指向新加入的節點,而新加入的節點指向原來前驅指向的節點;若是是在尾端插入,則待插入節點的前驅節點指向新加入的節點,而新加入的節點指向頭節點(見下圖)。

由上圖可知,插入節點D,B、C、D三者之間的關係爲
current爲插入節點的前驅節點
// 中間
current->next = new // B節點指向新節點D
new->next = current->next // 新節點D指向B的後繼節點C
// 尾端
current->next = new // C節點指向新節點D
new->next = header // 新節點D指向頭節點Header
刪除:若是刪除的是中間元素,則與單鏈表操做相同,將待刪除節點的前驅節點指向待刪除節點的後繼節點;若是刪除的是尾端元素,則將待刪除節點的前驅節點指向頭節點。

由上圖可知,刪除節點時,B、C、D三者之間的關係爲
current爲要刪除節點的前驅節點
// 中間
current->next = current->next->next // A節點指向C節點
// 尾端
current->next = header // B節點指向頭節點Header
具體代碼以下:
View Code


<?php // 節點類 class Node { public $data; // 節點數據 public $previous; public $next; // 下一節點 public function __construct($data) { $this->data = $data; $this->next = NULL; } } // 循環鏈表類 class CircularLinkedList { private $header; // 頭節點 function __construct($data) { $this->header = new Node($data); $this->header->next = $this->header; } // 查找節點 public function find($item) { $current = $this->header; while ($current->data != $item) { $current = $current->next; } return $current; } // 插入新節點 public function insert($item, $new) { $newNode = new Node($new); $current = $this->find($item); if ($current->next != $this->header) { // 鏈表中間 $current->next = $newNode; $newNode->next = $current->next; } else { // 鏈表尾端 $current->next = $newNode; $newNode->next = $this->header; } return true; } // 刪除節點 public function delete($item) { $current = $this->header; while ($current->next != null && $current->next->data != $item) { $current = $current->next; } if ($current->next != $this->header) { // 鏈表中間 $current->next = $current->next->next; } else { // 鏈表尾端 $current->next = $this->header; } } // 顯示鏈表中的元素 public function display() { $current = $this->header; while ($current->next != $this->header) { echo $current->next->data . "  "; $current = $current->next; } } } // 測試 $linkedList = new CircularLinkedList('header'); $linkedList->insert('header', 'China'); $linkedList->insert('China', 'USA'); $linkedList->insert('USA', 'England'); $linkedList->insert('England', 'Australia'); echo '鏈表爲:'; $linkedList->display(); echo "</br>"; echo '-----刪除節點USA-----'; echo "</br>"; $linkedList->delete('USA'); echo '鏈表爲:'; $linkedList->display(); // 輸出: 鏈表爲:China USA England Australia -----刪除節點USA----- 鏈表爲:China England Australia
原文地址:https://www.cnblogs.com/sunshineliulu/p/7717301.html3d