: This article mainly introduces the linked list, stack, and queue of the php Standard Library spl. if you are interested in the PHP Tutorial, refer to it. Bidirectional linked list: spldoublyshortlist
1. add and delete nodes
Push: insert a node to the end of the linked list
Pop: get the tail node in the linked list and delete it from the linked list. The operation does not change the position of the current pointer.
Unshift: insert a node to the head of the linked list
Shift: deletes a linked list header node.
2. pointer operation method
Rewind: point the current pointer of the linked list to the head of the linked list (that is, bottom)
Current: to obtain the elements pointed to by the current node pointer of the linked list, you must call rewind before calling. After a node is deleted, it points to an empty node.
Next: let the current pointer of the linked list point to the next node, and the returned value of curent changes accordingly.
Bottom: obtains the element in the head of the linked list. The current pointer position remains unchanged.
Top: get the end element of the linked list. The current pointer position remains unchanged.
3. other methods (for usage, refer to the stack class)
Valid: checks whether there are still nodes in the linked list. it can be used to judge the cyclic output.
Count: count the number of nodes in the linked list.
Key: return the key value of the current node.
OffsetSet: set the value of the specified key. note: If the key is 0, 0 points to the header (bottom) in the linked list and points to the top of the stack in the stack.
Offunset: cancels the value of the specified key.
Push ('B'); $ obj-> push ('C'); $ obj-> unshift ('A'); var_dump ($ obj ); /* array (3) {[0] => string (1) "a" [1] => string (1) "B" [2] => string (1) "c"}) */$ obj-> rewind (); var_dump ($ obj-> current (); // string (1) "a" $ obj-> next (); var_dump ($ obj-> current (); // string (1) "a" var_dump ($ obj-> bottom (); // string (1) "a" var_dump ($ obj-> top (); // string (1) "c" var_dump ($ obj-> pop (); // string (1) "c" var_dump ($ obj);/** array (2) {[0] => string (1) "a" [1] => string (1) "B"} */var_dump ($ obj-> shift ()); // string (1) "a" var_dump ($ obj);/** array (1) {[0] => string (1) "B "}*/
Stack class: the SplStack class inherited from the spldoublyjavaslist class.
Principle: the underlying layer of the stack class is implemented by the stack, and the stack is an advanced and post-generated data structure. Therefore, some methods of the SplStack class inherited from the spldoublyshortlist class have different understandings, for example, in the rewind method, after spl uses the rewind method, the pointer points to the top of the stack in the figure. the push and pop operations are the top elements of the stack, and the unshift and shift operations are the bottom elements of the stack.
Push ('A'); $ stack-> push ('B'); $ stack-> push ('C'); echo $ stack-> count (); // 3 $ stack-> rewind (); echo $ stack-> current (); // c $ stack-> offsetSet (0, 'D '); // in offsetSet, 0 points to the top of the stack in the figure, increasing from top to bottom by 1, 2, 3, and 4 while ($ stack-> valid ()) {echo $ stack-> key (). "-> ". $ stack-> current (); $ stack-> next ();}/* 2-> d1-> b0-> */
Queue class: the SplQueue class inherited from the spldoublydetaillist class
Enqueue: Enter the queue
Dequeue: exit the queue
The rewind and offsetSet methods of the queue class are similar to linked lists.
Enqueue ('A'); $ obj-> enqueue ('B'); $ obj-> enqueue ('C'); var_dump ($ obj ); /* [0] => string (1) "a" [1] => string (1) "B" [2] => string (1) "c"} */$ obj-> unshift ("d"); $ obj-> push ('e'); var_dump ($ obj ); /** array (5) {[0] => string (1) "d" [1] => string (1) "a" [2] => string (1) "B" [3] => string (1) "c" [4] => string (1) "e"} */$ obj-> rewind (); echo $ obj-> current (); // d $ obj-> offsetSet (0, 'H '); var_dump ($ obj);/** array (5) {[0] => string (1) "h" [1] => string (1) "a" [2] => string (1) "B" [3] => string (1) "c" [4] => string (1) "e "}*/
Copyright Disclaimer: This article is an original article by the blogger and cannot be reproduced without the permission of the blogger.
The above introduces the linked list, stack, and queue of the php Standard Library spl, including some content, and hope to help friends who are interested in PHP tutorials.