PHP Standard library SPL list, stack, queue

Source: Internet
Author: User
Tags spl rewind
Doubly linked list class: Spldoublylinkedlist

1. How to delete nodes

Push: Inserts a node into the tail of the list
Pop: Gets the tail node in the linked list and removes the node from the linked list; The operation does not change the position of the current pointer
Unshift: Inserting a node into the head of a linked list
Shift: Delete a linked list head node

2. Pointer operation method

Rewind: Causes the current pointer of the list to point to the head of the linked list (that is, bottom)
Current: Gets the element that the linked list node pointer points to, and must call rewind before calling. When a pointed node is deleted, it points to an empty node
Next: Let the current pointer of the list point to the next node, and the return value of curent changes
Bottom: Gets the list header element with the current pointer position unchanged
Top: Get the trailing element of the linked list, the current pointer position is unchanged

3. Other methods (see Stack class for usage)

Valid: Check if there are still nodes in the list, and the loop output can be used as a judgment
Count: Count the number of nodes in the list
Key: Returns the value of the current node
Offsetset: Sets the value of the specified key, note: If the key is 0, in the linked list 0 points to the head is bottom, in the stack to point to the top of the heap.
Offunset: Unregister the value of the specified key


   Php/** * Created by Matting * User: Matting * DATE:2015/8/5 * time:10:52 * *$obj=NewSpldoublylinkedlist ();$obj -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 classes: Splstack classes that inherit from the Spldoublylinkedlist class

Principle: The Stack class is the bottom layer is implemented by the stack is an advanced data structure, so some of the Splstack class inherited from the Spldoublylinkedlist class methods have some understanding of the different, such as the Rewind method, SPL uses the Rewind method after the pointer points to the top of the stack, the push and pop operations are the top elements of the stack, and the unshift and shift operations are the stack-bottom elements

 !--? PHP /** * Created by Matting * User: Matting * DATE:2015/8/5 * time:11:47 */  $stack   =   n EW  splstack ();  $stack  ->  push ( ' a ' );  $stack  ->  push ( ' B ' );  $stack  ->  push ( ' C ' ); Echo  $stack  ->  count (); //3   $stack  ->  rewind (); Echo  $stack  ->  current (); //c   $stack  ->  offsetset ( 0 ,  ' d ' ); //offsetset 0 points to the top of the stack in the diagram, and is incremented by the top of the stack 1,2,3,4   while  ( $stack  ->  valid ()) {echo  $stack  ->  key () .  ,  .   $stack  ->  current ();  $stack  ->  next (); /*2->d1->b0->a*/  

Queue classes: Splqueue classes that inherit from the Spldoublylinkedlist class

Enqueue: Entering the queue
Dequeue: Exit queue
Rewind,offsetset such as queue classes are similar to linked lists


  ByMatting *User: Matting *Date: -/8/5* Time: A: $*/$obj =NewSplqueue (); $obj->enqueue (' A '); $obj->enqueue (' B '); $obj->enqueue (' C '); Var_dump ($obj);/* [0]=String1)"a"[1]=String1)"B"[2]=String1)"C"}*/$obj->unshift ("D"); $obj->push (' E '); Var_dump ($obj);/** Array (5) {    [0]=String1)"D"[1]=String1)"a"[2]=String1)"B"[3]=String1)"C"[4]=String1)"E"}*/$obj->rewind (); Echo $obj->current ();//D$obj->offsetset (0,' h '); Var_dump ($obj);/** Array (5) {    [0]=String1)"H"[1]=String1)"a"[2]=String1)"B"[3]=String1)"C"[4]=String1)"E"}*/

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

The above describes the PHP standard library SPL list, stack, queue, including the aspects of the content, I hope to be interested in PHP tutorial friends helpful.

  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.