SplDoublyLinkedList in PHP

PHP

Below are important terms for understanding the concept of a doubly linked list:
  • Link : Each node in a linked list can store a pointer to another element, called a link.
  • Next : Each node in the linked list contains a link to the next node named Next.
  • Prev : Each node in the linked list contains a link to a previous node named Prev.
You can refer to the article at Doubly linked list (Introduction) for a more detailed understanding of doubly linked list.Class SplDoublyLinkedList- this is a PHP library class that provides the basic functions of a doubly linked list in PHP.Following are some of the basic functions of the SplDoublyLinkedList class:
  • push(): this function is used to insert a new node at the end of a doubly linked list. 
    Syntax :
    list_name.push (value);
    This will push the value to the end of the list_name.
  • pop(): This function is used to remove a node from the end of a doubly linked list. 
    Syntax :
    list_name.pop()
    This will remove the last node from the list_name list.
  • top(): This function is used to select a node at the end of a doubly linked list. 
    Syntax :
    list_name.top()
    This will return the last node from the list_name list.
  • bottom(): This function is used to select a node at the beginning of a doubly linked list. 
    Syntax :
    list_name.bottom()
    This will return the node present at the beginning of the list_name list.
  • add(): this function is used to insert a new value at the specified index into a double linked list, this also causes the previous value at this index (and all subsequent values) to be shuffled up the list ... 
    Syntax :
    list_name.add (`index`," value ");
    This will add the value to the index position in the list_name.
  • < b> count(): This function is used to count the number of elements present in a given doubly linked list. 
    Syntax :
    list.count()
    This will return a count of the total number of items present in the list_name list.
Below the program illustrates the above functions for creating and using a doubly linked list:
Program :
// Program for implementing basic PHP functions
/ / for a doubly linked list 
// Create an object of the SplDoublyLinkedList class $dlist = new SplDoublyLinkedList();  
// Insert items at the end of the list $dlist -> push ( `engineer` ); $dlist -> push ( `for` ); $dlist -> push ( `practice` );  
// Display the list echo "Original List:" ; for ( $dlist -> rewind (); $dlist -> valid(); $dlist -> next()) { echo $dlist -> current(). " " ; }  
// Remove an item from the end of the list $dlist -> pop();  
// Add a new element at the specified index
// Add code to index 2 $dlist -> add (2, "code" );  
// Display updated list echo "Updated List:" ; for ( $dlist -> rewind (); $dlist -> valid(); $dlist -> next()) { echo $dlist -> current(). " " ; }  
// Print the number of nodes echo "Count =" . $dlist -> count (). "" ;  
// Print the node to the top of the list echo "Top =" . $dlist -> top(). "" ;  
// Print node at the bottom of the list echo "Bottom =" . $dlist -> bottom(). "" ;  
?>
Output:
Original List: engineer for practice Updated List: engineer for code Count = 3 Top = code Bottom = engineer




Get Solution for free from DataCamp guru