Introduction to PHP Double-linked list (spldoublylinkedlist) and examples _php example

Source: Internet
Author: User
Tags mixed rewind

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

The Spldoublylinkedlist class in the PHP SPL provides an operation on a double linked list.
The Spldoublylinkedlist class summary is as follows:

Spldoublylinkedlist implements iterator, arrayaccess, countable {public __construct (void) public void add (Mixed $index, mixed $newval)//The head node of the double linked list public mixed top (void)//Double linked list of the tail node public mixed bottom (void)//  The number of doubly-linked table elements is public int count (void)//detect whether the 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 present (void) Point the pointer at the beginning of the iteration with public void rewind (void)/check if the double linked list has nodes public bool valid (void)//Specify whether the index node exists public Bo  Ol offsetexists (mixed $index)//Get the specified index node value public mixed Offsetget (mixed $index)//set at the specified index value public void
  Offsetset (mixed $index, mixed $newval)//delete at specified index node public void Offsetunset (mixed $index)//from the tail of the double linked list pop-up elements Public mixed pop (void)//add element to the tail of a doubly-linked list (mixed $value)/serialization store public string serialize (VO ID)//deserialization public VOID unserialize (String $serialized)//Set Iteration mode public void Setiteratormode (int $mode)//Get Iteration pattern Spldoublylinkedl
 
  Ist::it_mode_lifo (Stack style) Spldoublylinkedlist::it_mode_fifo (Queue style) public int getiteratormode (void) Double-linked list head remove element public mixed shift (void)//Double linked table header add element 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 Array (
  ' Pop ' => $list->pop (),
  ' count ' => $list->count (),
  ' IsEmpty ' => $list-> IsEmpty (),
  ' bottom ' => $list->bottom (),
  ' top ' => $list->top ())
;
 
$list->setiteratormode (SPLDOUBLYLINKEDLIST::IT_MODE_FIFO);
Print_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);
Print_r (Array (
  ' offsetexists ' => $list->offsetexists (4),
  ' Offsetget ' => $list->offsetget (0),
 
));
Print_r ($list);

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.