Doubly linked list class: SplDoublyLinkedList
push: Insert a node to the end of the linked list
pop: Get the tail node in the linked list and delete this node 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: delete a linked list head node
rewind: make the current pointer of the linked list point to the head of the linked list (i.e. bottom)
current: Get the element pointed to by the current node pointer of the linked list. Rewind must be called before calling. When the pointed node is deleted, it will point to an empty node
next: Let the current pointer of the linked list point to the next node, and the return value of curent changes accordingly
bottom: Get the head element of the linked list, the current pointer position remains unchanged
top: Get the tail element of the linked list, the current pointer position remains unchanged
valid: Check whether there are still nodes in the linked list, it can be used as a judgment when looping output
count: counts the number of nodes in the linked list
key: Returns 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 head or bottom in the linked list, and points to the top of the stack in the stack.
offunset: Unregister the value of the specified key
<code><span><?</span>php <span>/** * Created by 马廷广 * User: 马廷广 * Date: 2015/8/5 * Time: 10:52 */</span><span>$obj</span><span>=</span><span>new</span> SplDoublyLinkedList(); <span>$obj</span><span>-></span>push(<span>'b'</span>); <span>$obj</span><span>-></span>push(<span>'c'</span>); <span>$obj</span><span>-></span>unshift(<span>'a'</span>); var_dump(<span>$obj</span>); <span>/* array(3) { [0]=> string(1) "a" [1]=> string(1) "b" [2]=> string(1) "c" } ) */</span><span>$obj</span><span>-></span>rewind(); var_dump(<span>$obj</span><span>-></span>current());<span>//string(1) "a"</span><span>$obj</span><span>-></span>next(); var_dump(<span>$obj</span><span>-></span>current());<span>//string(1) "a"</span> var_dump(<span>$obj</span><span>-></span>bottom());<span>//string(1) "a"</span> var_dump(<span>$obj</span><span>-></span>top());<span>//string(1) "c"</span> var_dump(<span>$obj</span><span>-></span>pop());<span>//string(1) "c"</span> var_dump(<span>$obj</span>); <span>/* * array(2) { [0]=> string(1) "a" [1]=> string(1) "b" } */</span> var_dump(<span>$obj</span><span>-></span>shift());<span>//string(1) "a"</span> var_dump(<span>$obj</span>); <span>/* * array(1) { [0]=> string(1) "b" } */</span></code>
Stack class: SplStack class inherited from SplDoublyLinkedList class
Principle: The bottom layer of the stack class is implemented by the stack. The stack is a first-in, last-out data structure, so some of the SplStack class The methods inherited from the SplDoublyLinkedList class have some differences in understanding, such as the rewind method. After spl uses the rewind method, the pointer will point to the top of the stack in the picture. push and pop operate on the top elements of the stack, and unshift and shift operate on the bottom elements of the stack.
<code><span><?</span>php <span>/** * Created by 马廷广 * User: 马廷广 * Date: 2015/8/5 * Time: 11:47 */</span><span>$stack</span><span>=</span><span>new</span> SplStack(); <span>$stack</span><span>-></span>push(<span>'a'</span>); <span>$stack</span><span>-></span>push(<span>'b'</span>); <span>$stack</span><span>-></span>push(<span>'c'</span>); echo <span>$stack</span><span>-></span>count();<span>//3</span><span>$stack</span><span>-></span>rewind(); echo <span>$stack</span><span>-></span>current();<span>//c</span><span>$stack</span><span>-></span>offsetSet(<span>0</span>,<span>'d'</span>);<span>//offsetSet中0指向的是图中的栈顶,由栈顶向下递增1,2,3,4</span><span>while</span>(<span>$stack</span><span>-></span>valid()){ echo <span>$stack</span><span>-></span>key()<span>.</span><span>"->"</span><span>.</span><span>$stack</span><span>-></span>current(); <span>$stack</span><span>-></span>next(); } <span>/*2->d 1->b 0->a */</span></code>
Queue class: SplQueue class inherited from SplDoublyLinkedList class
enqueue: Enter the queue
dequeue: Exit the queue
The rewind, offsetSet and other methods of the queue class are similar to the linked list
<code><?php /** * Created <span>by</span> 马廷广 * <span>User</span>: 马廷广 * <span>Date</span>: <span>2015</span>/<span>8</span>/<span>5</span> * <span>Time</span>: <span>12</span>:<span>36</span> */ $obj = <span>new</span> SplQueue(); $obj->enqueue(<span>'a'</span>); $obj->enqueue(<span>'b'</span>); $obj->enqueue(<span>'c'</span>); var_dump($obj); /* [<span>0</span>]<span>=></span> string(<span>1</span>) <span>"a"</span> [<span>1</span>]<span>=></span> string(<span>1</span>) <span>"b"</span> [<span>2</span>]<span>=></span> string(<span>1</span>) <span>"c"</span> } */ $obj->unshift(<span>"d"</span>); $obj->push(<span>'e'</span>); var_dump($obj); /* * array(<span>5</span>) { [<span>0</span>]<span>=></span> string(<span>1</span>) <span>"d"</span> [<span>1</span>]<span>=></span> string(<span>1</span>) <span>"a"</span> [<span>2</span>]<span>=></span> string(<span>1</span>) <span>"b"</span> [<span>3</span>]<span>=></span> string(<span>1</span>) <span>"c"</span> [<span>4</span>]<span>=></span> string(<span>1</span>) <span>"e"</span> } */ $obj->rewind(); echo $obj->current();<span>//</span>d $obj->offsetSet(<span>0</span>,<span>'h'</span>); var_dump($obj); /* * array(<span>5</span>) { [<span>0</span>]<span>=></span> string(<span>1</span>) <span>"h"</span> [<span>1</span>]<span>=></span> string(<span>1</span>) <span>"a"</span> [<span>2</span>]<span>=></span> string(<span>1</span>) <span>"b"</span> [<span>3</span>]<span>=></span> string(<span>1</span>) <span>"c"</span> [<span>4</span>]<span>=></span> string(<span>1</span>) <span>"e"</span> } */</code>
Copyright statement: This article is an original article by the blogger and may not be reproduced without the permission of the blogger.
The above has introduced the linked lists, stacks, and queues of the PHP standard library spl, including aspects of it. I hope it will be helpful to friends who are interested in PHP tutorials.