Data structures: SplDoublyLinkedList

Keywords: PHP


The double linked list is an important linear storage structure. For each node in the double linked list, it not only stores its own information, but also stores the addresses of the predecessor and successor nodes.


SplDoublyLinkedList  implements Iterator   , ArrayAccess   , Countable   {
    public __construct  ( void )
    public void add  ( mixed  $index  , mixed  $newval  )
    //Head node of double linked list
    public mixed top  ( void )
    //Tail node of double linked list
    public mixed bottom  ( void )
    //The number of elements in the double table
    public int count  ( void )
    //Check whether double linked list is empty
    public bool isEmpty  ( void )
    //Current node index
    public mixed key  ( void )
    //Move to previous record
    public void prev  ( void )
    //Move to 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 nodes in double linked list
    public bool valid  ( void )
    //Specifies whether the node at 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 a double linked list
    public mixed pop  ( void )
    //Add elements to the end of a double linked list
    public void push  ( mixed  $value  )
    //Serialized storage
    public string serialize  ( void )
    //De serialization
    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 )
    //Removing elements from the head of a double linked list
    public mixed shift  ( void )
    //Adding elements to the head of double linked list
    public void unshift  ( mixed  $value  )
  • The Iterator interface is implemented, which can realize iteration quickly;
  • The ArrayAccess interface is implemented to access linked list data as an array;
$list = new SplDoublyLinkedList();

# Methods to see the name can understand, mainly introduces the following places;
  # List structure at this time
  [0] => a
  [1] => b
  [2] => c
  [3] => d


// Because of the implementation of the interface ArrayAccess, data can be manipulated like array;
echo $list[2];

  # List structure at this time
  [0] => a
  [1] => z
  [2] => b
  [3] => c
  [4] => d

//Set an iteration mode for iteration ↓;
$iteratorMode = $list->getIteratorMode(); //Get the current iteration mode

  # About mode
  IT_MODE_LIFO: Stack style, LIFO, heap structure
  IT_MODE_FIFO: Queue style, First in, first out, queue structure (default)
  IT_MODE_DELETE: Elements are deleted by the iterator Delete while iterating
  IT_MODE_KEEP: Elements are traversed by the iterator Normal iteration, do not delete (default)

// ↓ set whether to delete elements during iteration
for ($list->rewind(); $list->valid(); $list->next()) {
  echo $list->current()."\n";

for ($list->rewind(); $list->valid(); $list->next()) {
  echo $list->current()."\n";

If you can understand the names in other method manuals, you will not be able to explain:

Posted by bodge on Fri, 08 Nov 2019 06:29:41 -0800