This article mainly introduces the relevant information about PHP Doubly Linked List (SplDoublyLinkedList) and usage examples. Friends who need it can refer to it. Next
Doubly linked list is an important linear storage structure. For each node in the doubly linked list, it not only stores its own information, but also stores the addresses of the predecessor and successor nodes.
The SplDoublyLinkedList class in PHP SPL provides operations on doubly linked lists.
The SplDoublyLinkedList class summary is as follows:
?
|
SplDoublyLinkedList implements Iterator , ArrayAccess , Countable { public __construct (void) public void add ( mixed $index , mixed $newval ) //The head node of the double linked list public mixed top (void) //Tail node of double linked list public mixed bottom (void) //The number of double table elements public int count (void) //Check whether the double linked list is empty public bool isEmpty (void) //Current node index public mixed key (void) //Move to the previous record public void prev (void) //Move to the next record public void next (void) //Current record public mixed current (void) //Point the pointer to the beginning of the iteration public void rewind (void) //Check whether there are still nodes in the double linked list public bool valid (void) //Whether the node at the specified index exists public bool offsetExists ( mixed $index ) //Get the node value at the specified index public mixed offsetGet ( mixed $index ) //Set the value at the specified index public void offsetSet ( mixed $index , mixed $newval ) //Delete the node at the specified index public void offsetUnset ( mixed $index ) //Pop elements from the end of the double linked list public mixed pop (void) //Add elements to the end of the double linked list public void push (mixed $value) //Serialized storage public string serialize (void) //Deserialization public void unserialize ( string $serialized ) //Set iteration mode public void setIteratorMode ( int $mode ) //Get the iteration mode SplDoublyLinkedList::IT_MODE_LIFO (Stack style) SplDoublyLinkedList::IT_MODE_FIFO (Queue style) public int getIteratorMode (void) //Remove elements from the head of the double linked list public mixed shift (void) //Add elements to the head of the double linked list public void unshift ( mixed $value ) } |
It is also easy to use
?
10 11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
|
$list = new SplDoublyLinkedList(); $list->push('a'); $list->push('b'); $list->push('c'); $list->unshift('top'); $list->shift(); print_r(array( 'pop' => $list->pop(), 'count' => $list->count(), 'isEmpty' => $list->isEmpty(), 'bottom' => $list->bottom(), 'top' => $list->top() )); $list->setIteratorMode(SplDoublyLinkedList::IT_MODE_FIFO); print_r($list->getIteratorMode()); for($list->rewind(); $list->valid(); $list->next()) { echo $list->current().PHP_EOL; } print_r($a = $list->serialize()); //print_r($list->unserialize($a)); $list->offsetSet(0,'new one'); $list->offsetUnset(0); print_r(array( 'offsetExists' => $list->offsetExists(4), 'offsetGet' => $list->offsetGet(0), )); print_r($list); |