PHP double-linked list of detailed examples

Source: Internet
Author: User
Tags rewind
This article mainly introduces the PHP doubly linked list (spldoublylinkedlist) Introduction and use examples of relevant data. We hope to help you.

Doubly linked list is an important linear storage structure, for each node in a doubly linked list, not only to store their own information, but also to save the predecessor and the successor node address.

The Spldoublylinkedlist class in PHP SPL provides the operation of a doubly linked list.
The Spldoublylinkedlist class is summarized as follows:


Spldoublylinkedlist implements Iterator, arrayaccess, countable {public __construct (void) public void Add (mi  Xed $index, mixed $newval)//The head node of the doubly-linked list public mixed top (void)//The tail node of the doubly linked list public mixed bottom (void)//number of doubly-linked table elements public int count (void)//detect double-linked list is empty public bool IsEmpty (void)//current node index public mixed key (void)//move to previous record Pu Blic void prev (void)//move to the next record public void Next (void)//The current record public mixed present (void)//pointer to public at the beginning of the iteration void Rewind (void)//Check whether a doubly linked list has a node public bool valid (void)//Specify whether the node at index has a public bool offsetexists (mixed $inde  x)//Gets 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 specified index node public void Offsetunset (mixed $index)//eject element from the tail of the doubly linked list public mixed pop (void)//add element to double chain The end of the table, public void push (mixed $value)//serialization store public string serialize (void)//deserialization public void unserialize (str ing $serialized)//SetIteration mode public void Setiteratormode (int $mode)//Get Iteration pattern Spldoublylinkedlist::it_mode_lifo (Stack style) Spldoublylinkedli St::it_mode_fifo (Queue style) public int getiteratormode (void)//The head of the doubly linked list removes the element public mixed shift (void)//double-linked list header Tim CAD public void Unshift (mixed $value)}


It's easy to use.



$list = new Spldoublylinkedlist (), $list->push (' a '), $list->push (' B '); $list->push (' C '); $list->unshift (' top '); $list->shift (); Print_r (  ' pop ' = $list->pop (),  ' count ' = = $list->count (),  ' isEmpty ' + $list IsEmpty (),  ' bottom ' = $list->bottom (),  ' top ' = $list->top ())); $list->setiteratormode (Spldoublylinkedlist::it_mode_fifo);p Rint_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);p Rint_r (Array (  ' offsetexists ' = $list Offsetexists (4),  ' offsetget ' = $list->offsetget (0),));p Rint_r ($list);


Related recommendations:

Example of PHP member ranking function based on bidirectional linked list and sort operation

PHP implementation of single-linked list rollover Operation example

PHP Simple implementation of circular list function example

Related Article

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.