鏈表是由一組節點組成的集合。每個節點都使用一個對象的引用指向它的后繼。指向另一個節點的引用叫做鏈。
鏈表分為單鏈表、雙鏈表、循環鏈表。
一、單鏈表
插入:鏈表中插入一個節點的效率很高。向鏈表中插入一個節點,需要修改它前面的節點(前驅),使其指向新加入的節點,而新加入的節點則指向原來前驅指向的節點(見下圖)。
由上圖可知,B、C之間插入D,三者之間的關系為
current為插入節點的前驅節點
new->next = current->next // 新節點D指向B的后繼節點C
current->next = new // B節點指向新節點D
刪除:從鏈表中刪除一個元素,將待刪除元素的前驅節點指向待刪除元素的后繼節點,同時將待刪除元素指向 null,元素就刪除成功了(見下圖)。
由上圖可知,A、C之間刪除B,三者之間的關系為
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
二、雙鏈表
單鏈表從鏈表的頭節點遍歷到尾節點很簡單,但從后向前遍歷就沒那么簡單了。它的每個數據結點中都有兩個指針,分別指向直接后繼和直接前驅。
所以,從雙向鏈表中的任意一個結點開始,都可以很方便地訪問它的前驅結點和后繼結點。

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

由上圖可知,B、C之間插入D,三者之間的關系為
current為插入節點的前驅節點
new->next = current->next // 新節點D的next屬性指向B的后繼節點C
current->next->previous = new // B的后繼節點C的previous屬性指向新節點D(原先是C的previous屬性指向B)
current->next = new // B的next屬性指向新節點D
new->previous = current // B的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
三、循環鏈表
循環鏈表和單向鏈表相似,節點類型都是一樣的。唯一的區別是,在創建循環鏈表時,讓其頭節點的 next 屬性指向它本身,即:head.next = head,這種行為會傳導至鏈表中的每個節點,使得每個節點的 next 屬性都指向鏈表的頭節點。換句話說,鏈表的尾節點指向頭節點,形成了一個循環鏈表。

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

由上圖可知,插入節點D,B、C、D三者之間的關系為
current為插入節點的前驅節點
// 中間
new->next = current->next // 新節點D指向B的后繼節點C
current->next = new // B節點指向新節點D
// 尾端
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
具體代碼如下:
<?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
注:循環鏈表還可以分為單循環鏈表和雙循環鏈表,這里只實現了單循環鏈表。