The SplDoublyLinkedList::add() function is an inbuilt function in PHP which is used to add a new value at the given index.
Syntax:
php
php
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.
<?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:
Program 2:
SplDoublyLinkedList Object
(
[flags:SplDoublyLinkedList:private] => 0
[dllist:SplDoublyLinkedList:private] => Array
(
[0] => 1
[1] => Geeks
[2] => G
[3] => 10
)
)
<?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:
Reference: https://www.php.net/manual/en/spldoublylinkedlist.add.php
SplDoublyLinkedList Object
(
[flags:SplDoublyLinkedList:private] => 0
[dllist:SplDoublyLinkedList:private] => Array
(
[0] => 30
[1] => 20
[2] => 30
[3] => Geeks
[4] => G
)
)