Open In App

PHP SplDoublyLinkedList add() Function

Improve
Improve
Like Article
Like
Save
Share
Report

The SplDoublyLinkedList::add() function is an inbuilt function in PHP which is used to add a new value at the given index.

Syntax:

void SplDoublyLinkedList::add( $index, $newval )

Parameters: It contains two parameters as mentioned above and described below:

  • $index: It holds the index value where the new element is to be inserted.
  • $newval: It holds the element which to be inserted or added.

Return Value: It does not return any value.

Below programs illustrate the SplDoublyLinkedList::add() function in PHP:

Program 1:




<?php 
  
// Declare an empty SplDoublyLinkedList
$list = new \SplDoublyLinkedList;
  
// Use SplDoublyLinkedList::add() function to 
// add elements to the SplDoublyLinkedList
$list->add(0, 1); 
  
$list->add(1, "Geeks"); 
  
$list->add(2, "G"); 
  
$list->add(3, 10); 
  
print_r($list); 
?> 


Output:

SplDoublyLinkedList Object
(
    [flags:SplDoublyLinkedList:private] => 0
    [dllist:SplDoublyLinkedList:private] => Array
        (
            [0] => 1
            [1] => Geeks
            [2] => G
            [3] => 10
        )

)

Program 2:




<?php 
  
// Declare an empty SplDoublyLinkedList
$list = new \SplDoublyLinkedList;
  
// Use SplDoublyLinkedList::add() function to 
// add elements to the SplDoublyLinkedList
$list->add(0, 30);
$list->add(1, 20);
$list->add(2, 30);
$list->add(3, "Geeks");
$list->add(4, 'G');
  
print_r($list); 
?> 


Output:

SplDoublyLinkedList Object
(
    [flags:SplDoublyLinkedList:private] => 0
    [dllist:SplDoublyLinkedList:private] => Array
        (
            [0] => 30
            [1] => 20
            [2] => 30
            [3] => Geeks
            [4] => G
        )

)

Reference: https://www.php.net/manual/en/spldoublylinkedlist.add.php



Last Updated : 23 Jun, 2023
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads