User Contributed Notes 4 notes

/*php doubly link list is an amazing data structure ,doubly means you can traverse forward as well as backward, it can act as a deque(double ended queue) if you want it to,here is how it works

*/

//instantiating an object of doubly link list

$dlist=new SplDoublyLinkedList();

//a push inserts data at the end of the list $dlist->push('hiramariam');$dlist->push('maaz');$dlist->push('zafar');

/* the list containshiramariammaazzafar*/

//while an unshift inserts an object at top of the list$dlist->unshift(1);$dlist->unshift(2);$dlist->unshift(3);

/* the list now contains321hiramariammaazzafar*/

//you can delete an item from the bottom of the list by using pop$dlist->pop();

/* the list now contains321hiramariammaaz

*/ //you can delete an item from the top of the list by using shift()$dlist->shift();

/* the list now contains

21hiramariammaaz

*/

/* if you want to replace an item at particular index you can use a method named add , note that if you want to replace an item that does not exist , an exception will be thrown*/

$dlist->add(3 , 2.24);

/*to go through the list we use a simple for loop, the rewind() method shown below point to the initials of the list depending on the iterator, a valid() method checks whether a list is still valid or not , meaning it ensures the loop does not go on and on after we reach the last data in the list , and the next() method simply points to the next data in the list.

<?php$splDoubleLinkedList = new SplDoublyLinkedList();$splDoubleLinkedList->push('a');$splDoubleLinkedList->push('3');$splDoubleLinkedList->push('v');$splDoubleLinkedList->push('1');$splDoubleLinkedList->push('p');//First of all, we need rewind list$splDoubleLinkedList->rewind();//Use while, check if the list has valid nodewhile ($splDoubleLinkedList->valid()){//Print current node's valueecho $splDoubleLinkedList->current()."\n";//Turn the cursor to next node$splDoubleLinkedList->next();}?>