PHP - Ds Deque::insert() Function



ThePHPDs\Deque::insert()function is used to insert a value in the current deque at the specified index. An index is the position of the element in the deque, starting at 0 representing the first element, 1 representing the second element, and so on.

If the specified index value is invalid, which means negative or greater than the deque size, it will throw an "OutOfRangeException" exception.

Syntax

Following is the syntax of the PHP Ds\Deque::insert() function −

public Ds\Deque::insert(int $index, mixed ...$values): void 

Parameters

Following are the parameters of this function −

  • index − It represents an integer value that specifies the index at which to insert.
  • values − An integer values need to be inserted.

Return value

This function does not return any value; instead, it inserts a value into the deque.

Example 1

The following is the basic example of the PHP Ds\Deque::insert() function −

<?php
   $deque = new \Ds\Deque([2, 3, 4, 5]);
   echo "The deque elements are: \n";
   print_r($deque);
   $index = 0;
   $value = 1;
   echo "The given index and value is: ".$index.", ".$value;
   echo "\nThe deque after a new element is inserted: ";
   #using insert() function
   $deque->insert($index, $value);
   print_r($deque);
?>

Output

The above program displays the following output −

The deque elements are:
Ds\Deque Object
(
    [0] => 2
    [1] => 3
    [2] => 4
    [3] => 5
)
The given index and value is: 0, 1
The deque after a new element is inserted: Ds\Deque Object
(
    [0] => 1
    [1] => 2
    [2] => 3
    [3] => 4
    [4] => 5
)

Example 2

Following is another example of the PHP Ds\Deque::insert() function. We use this function to insert a value 40 at the specified index 3 in this deque ([10, 20, 30, 50]) −

<?php
   $deque = new \Ds\Deque(["Tutorials", "Point"]);
   echo "The elements of deque: \n";
   print_r($deque);
   $index = 2;
   $value = "India";
   echo "The given index and value is: ".$index.", ".$value;
   echo "\nThe deque after new elements inserted: \n";
   #using insert() function
   $deque->insert($index, $value);
   print_r($deque);
?>

Output

On executing the above program, it will generate the following output −

The elements of deque:
Ds\Deque Object
(
    [0] => Tutorials
    [1] => Point
)
The given index and value is: 2, India
The deque after new elements inserted:
Ds\Deque Object
(
    [0] => Tutorials
    [1] => Point
    [2] => India
)

Example 3

If the specified index value is "greater" than the deque size, this function will throw an "OutOfRangeException" as follows −

<?php
   $deque = new \Ds\Deque([10, 20, 30, 40]);
   echo "The original deque: \n";
   print_r($deque);
   $index = 5;
   $val = 50;
   echo "The given and value is: ".$index.", ".$val;
   echo "\nThe deque after inserting new value: \n";
   $deque->insert($index, $val);
   print_r($deque);
?>

Output

After executing the above program, the following output will be displayed −

The original deque:
Ds\Deque Object
(
    [0] => 10
    [1] => 20
    [2] => 30
    [3] => 40
)
The given and value is: 5, 50
The deque after inserting new value:
PHP Fatal error:  Uncaught OutOfRangeException: Index out of range: 
5, expected 0 <= x <= 3 in C:\Apache24\htdocs\index.php:9
Stack trace:
#0 C:\Apache24\htdocs\index.php(9): Ds\Deque->insert(5, 50)
#1 {main}
  thrown in C:\Apache24\htdocs\index.php on line 9
php_function_reference.htm
Advertisements