This article mainly introduces the PHP data structure on the basis of the double linked list, has a certain reference value, now share to everyone, the need for friends can refer to
What is a doubly linked list?
On a real-world PHP data structure based on a single linked list said
A
single-linked list consists of one object that is a node, each node has a pointer to the next node, and the last node's pointer field points to null. Each node can store any data type.
In the case of a doubly linked list, each node has two pointer fields, pointing to the precursor and subsequent nodes, respectively. Single-linked lists are unidirectional, and doubly-linked lists are bidirectional.
Common operations
Our common operations for doubly linked lists are as follows:
Insert
InsertBefore
InsertAfter
Insertatfirst
Insertatlast
Deletefirst
Deletelast
Delete
Reverse
Getnthnode
...
PHP Language Implementation
First we implement the ListNode class of a doubly linked list by definition.
Class listnode{public $data = null; public $next = null; public $prev = null; Public function __construct (string $data) { $this->data = $data; }}
Looking at the list class again, you first need 3 private attributes, namely head node, tail node and length.
Class doublelinkedlist{ private $head = null; Private $last = null; Private $length = 0;}
The next step is to see how to implement the first commonly used insert, which is an operation with an average time complexity of O (n).
/** * Inserting a node * @param string|null $data * @return bool * complexity O (n) */public function Insert (string $data = null) {
$newNode = new ListNode ($data); if ($this->head) { $currentNode = $this->head; while ($currentNode) { if ($currentNode->next = = = null) { $currentNode->next = $newNode; $newNode->prev = $currentNode; $this->last = $newNode; $this->length++; return true; } $currentNode = $currentNode->next; } } else { $this->head = & $newNode; $this->last = $newNode; $this->length++; return true; }}
Then see how to delete a node.
/** * Delete a node * @param string $data * @return bool| ListNode * Complexity O (n) */public function Delete (string $query = null) {if ($this->head) {$currentNode = $this-&G T;head; $prevNode = null; while ($currentNode) {if ($currentNode->data = = = $query) {if ($nextNode = $currentNode->next) { if ($prevNode) {$prevNode->next = $nextNode; $nextNode->prev = $prevNode; } else {$this->head = $nextNode; $nextNode->prev = null; } unset ($currentNode); } else {if ($prevNode) {$this->last = $prevNode; $prevNode->next = null; Unset ($currentNode); } else {$this->head = null; $this->last = null; }} $this->length--; Return true; } $prevNode = $currentNode; $currentNode = $currentNode->next; }} return false;}
Reversing a doubly linked list is not very complicated either.
Public Function reverse () { if ($this->head!== null) { if ($this->head->next!== null) { $ Reversedlist = null; $currentNode = $this->head; while ($currentNode!== null) { $next = $currentNode->next; $currentNode->next = $reversedList; $currentNode->prev = $next; $reversedList = $currentNode; $currentNode = $next; } $this->last = $this->head; $this->head = $reversedList;}}}
A detailed implementation of the other operations of the doubly linked list can be found here.
The doubly linked list is a kind of chain-access data structure, which is more special than the single-linked list, and it also belongs to the list structure, the single linked list, the ring linked list and the multi-linked list.
Special series
PHP Basic data Structure Special series directory address: https://github.com/... Mainly use PHP syntax to summarize the underlying data structures and algorithms. There are basic knowledge that we can easily ignore in our daily PHP development and some practical suggestions about specification, deployment and optimization in modern PHP development, as well as further research on JavaScript language features.
The above is the whole content of this article, I hope that everyone's learning has helped, more relevant content please pay attention to topic.alibabacloud.com!