什么是链表
链表(Linked List)是一种常见的线性结构。它不需要一块连续的内存空间,通过指针即可将一组零散的内存块串联起来。将那种碎片内存进行合理的利用,解决空间的问题。
我们把内存块存为链表的节点,为了将所有的节点串起来,每个链表的节点除了存储数据之外,还需要记录链表的下一个节点的地址,这个记录下个节点地址的指针我们叫做后驱指针。
搜索链表需要O(N)的时间复杂度,这点和数组类似,但是链表不能像数组一样,通过索引的方式以O(1)的时间读取第n个数。
链表的优势在于能够以较高的效率在任意位置插入或者删除一个节点。
链表是线性表的一种,所谓的线性表包含顺序线性表和链表,顺序线性表是用数组实现的,在内存中有顺序排列,通过改变数组大小实现。而链表不是用顺序实现的,用指针实现,在内存中不连续。链表有很多种不同的类型:单向链表、双向链表及循环链表。
链表类别
单向链表
每个节点有一个next指针指向后一个节点,还有一个成员变量用于存储数值。
单向链表中有两个节点比较特殊,分别是头结点和尾节点。
头结点用来记录链表的基地址,知道头结点我们就可以遍历得到整条链表。尾结点的特殊在于指针指向的是一个空指针NULL。
双向链表
双向链表支持两个方向,每个节点不只有一个后驱指针next指向后面的节点,还有一个前驱指针prev指向前面的节点。
循环链表
循环链表是一种特殊的单链表,与单链表不同的是尾节点不指向空地址,指向链表的头结点。
优点是从链尾到链头比较方便,当要处理的数据具有环形结构特点是,非常适合用循环链表来处理。

优缺点
优点:动态扩容,不需要占用过多的内存
缺点:不能随机访问,如果要访问一个元素的话,不能根据索引访问,只能从头开始遍历,找到对应的元素O(n)
代码实现
单向链表
<?php/*** 定义节点类,创建节点* Class Node*/class ListNode{public $data; // 节点数据public $next; // 下一节点public function __construct($data){$this->data = $data;}}/*** 单链表类* Class SingleLinkedList*/class SingleLinkedList{private $head = null;private $length = 0;private $currentNode;private $currentPosition;/*** 插入一个节点* @param string|null $data* @return bool* complexity O(n)*/public function insert(string $data = null){$newNode = new ListNode($data);if ($this->head === null) {$this->head = &$newNode;} else {$currentNode = $this->head;while ($currentNode->next !== null) {$currentNode = $currentNode->next;}$currentNode->next = $newNode;}$this->length++;return true;}/*** 在特定节点前插入* @param string $data* @param string $query* complexity O(n)*/public function insertBefore(string $data, string $query){$newNode = new ListNode($data);if ($this->head) {$previous = null;$currentNode = $this->head;while ($currentNode !== null) {if ($currentNode->data === $query) {$newNode->next = $currentNode;if (empty($previous)) {$newNode->next = $currentNode;$this->head = $newNode;} else {$previous->next = $newNode;}$this->length++;break;}$previous = $currentNode;$currentNode = $currentNode->next;}}}/*** 在特定节点后插入* @param string|null $data* @param string|null $query* complexity O(n)*/public function insertAfter(string $data = null, string $query = null){$newNode = new ListNode($data);if ($this->head) {$nextNode = null;$currentNode = $this->head;while ($currentNode !== null) {if ($currentNode->data === $query) {if ($nextNode !== null) {$newNode->next = $nextNode;$currentNode->next = $newNode;$this->length++;break;} else {$currentNode->next = $newNode;$currentNode->next = $newNode;$this->length++;break;}}$currentNode = $currentNode->next;$nextNode = $currentNode->next;}}}/*** 在最前方插入节点* @param string $data* complexity O(1)*/public function insertAtFirst(string $data){$newNode = new ListNode($data);if ($this->head === null) {$this->head = &$newNode;} else {$currentFirstNode = $this->head;$newNode->next = $currentFirstNode;$this->head = &$newNode;}$this->length++;}/*** 搜索一个节点* @param string $data* @return bool|ListNode* complexity O(n)*/public function search(string $data){if ($this->length > 0) {$currentNode = $this->head;while ($currentNode !== null) {if ($currentNode->data === $data) {return $currentNode;}$currentNode = $currentNode->next;}}return false;}/*** 删除最前面的节点* @return bool* complexity O(1)*/public function deleteFirst(){if ($this->head !== null) {if ($this->head->next !== null) {$this->head = $this->head->next;} else {$this->head = null;}$this->length--;return true;}return false;}/*** 删除最后面的节点* @return bool* complexity O(1)*/public function deleteLast(){if ($this->head !== null) {$currentNode = $this->head;if ($currentNode->next !== null) {$previousNode = null;while ($currentNode->next !== null) {$previousNode = $currentNode;$currentNode = $currentNode->next;}$previousNode->next = null;} else {$this->head = null;}$this->length--;return true;}return false;}/*** 删除特定节点* @param string $query* @return bool* complexity O(n)*/public function delete(string $query){if ($this->head !== null) {$currentNode = $this->head;$previous = null;while ($currentNode !== null) {if ($currentNode->data === $query) {if ($currentNode->next === null) {$previous->next = null;} else {$previous->next = $currentNode->next;}$this->length--;return true;}$previous = $currentNode;$currentNode = $currentNode->next;}}return false;}/***反转链表* complexity O(n)*/public function reverse(){if ($this->head !== null) {if ($this->head->next !== null) {$reveredList = null;$next = null;$currentNode = $this->head;while ($currentNode !== null) {$next = $currentNode->next;$currentNode->next = $reveredList;$reveredList = $currentNode;$currentNode = $next;}$this->head = $reveredList;}}}/*** 返回特定位置的节点* @param int $n* @return null* complexity O(n)*/public function getNthNode(int $n = 0){$count = 0;if ($this->head !== null && $n <= $this->length) {$currentNode = $this->head;while ($currentNode !== null) {if ($count === $n) {return $currentNode;}$count++;$currentNode = $currentNode->next;}}return false;}public function current(){return $this->currentNode->data;}public function next(){$this->currentPosition++;$this->currentNode = $this->currentNode->next;}public function rewind(){$this->currentPosition = 0;$this->currentNode = $this->head;}public function key(){return $this->currentPosition;}public function valid(){return $this->currentNode !== NULL;}public function getSize(){return $this->length;}public function display(){echo 'LinkList length: ' . $this->length . PHP_EOL;$currentNode = $this->head;while ($currentNode !== null) {echo $currentNode->data . PHP_EOL;$currentNode = $currentNode->next;}}}$linkedList = new SingleLinkedList();$linkedList->insert('China');$linkedList->insert('USA');$linkedList->insert('England');$linkedList->insert('Australia');echo '链表为:';$linkedList->display();$linkedList->reverse();echo PHP_EOL;echo '链表为:';$linkedList->display();
