Change language

SplDoublyLinkedList in 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.
  • 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

Shop

Learn programming in R: courses

$

Best Python online courses for 2022

$

Best laptop for Fortnite

$

Best laptop for Excel

$

Best laptop for Solidworks

$

Best laptop for Roblox

$

Best computer for crypto mining

$

Best laptop for Sims 4

$

Latest questions

NUMPYNUMPY

Common xlabel/ylabel for matplotlib subplots

12 answers

NUMPYNUMPY

How to specify multiple return types using type-hints

12 answers

NUMPYNUMPY

Why do I get "Pickle - EOFError: Ran out of input" reading an empty file?

12 answers

NUMPYNUMPY

Flake8: Ignore specific warning for entire file

12 answers

NUMPYNUMPY

glob exclude pattern

12 answers

NUMPYNUMPY

How to avoid HTTP error 429 (Too Many Requests) python

12 answers

NUMPYNUMPY

Python CSV error: line contains NULL byte

12 answers

NUMPYNUMPY

csv.Error: iterator should return strings, not bytes

12 answers


Wiki

Python | How to copy data from one Excel sheet to another

Common xlabel/ylabel for matplotlib subplots

Check if one list is a subset of another in Python

sin

How to specify multiple return types using type-hints

exp

Printing words vertically in Python

exp

Python Extract words from a given string

Cyclic redundancy check in Python

Finding mean, median, mode in Python without libraries

cos

Python add suffix / add prefix to strings in a list

Why do I get "Pickle - EOFError: Ran out of input" reading an empty file?

Python - Move item to the end of the list

Python - Print list vertically