PHP - Ds Deque::reverse() Function



The PHPDs\Deque::reverse() function is used to reverse the deque in-place. The term "in-place" refers that the function modifying the original deque instead of creating or allocating any new deque or a new space in the memory. When an empty deque is taken as input, the output is also an empty([])deque.

Syntax

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

public Ds\Deque::reverse(): void 

Parameters

This function does not accept any parameter.

Return value

This function does not return any value, instead it reverse the deque.

Example 1

The following program demonstrates the usage of the PHP Ds\Deque::reverse() function −

<?php
   $deque = new \Ds\Deque([12, 24, 36, 48, 60]);
   echo "The original deque: \n";
   print_r($deque);
   #using reverse() function
   $deque->reverse();
   echo "The deque after reverse: \n";
   print_r($deque);
?>

Output

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

The original deque:
Ds\Deque Object
(
    [0] => 12
    [1] => 24
    [2] => 36
    [3] => 48
    [4] => 60
)
The deque after reverse:
Ds\Deque Object
(
    [0] => 60
    [1] => 48
    [2] => 36
    [3] => 24
    [4] => 12
)

Example 2

Following is another example of the PHP Ds\Deque::reverse() function. We use this function to reverse this deque (['t', 'u', 't', 'o', 'r']) −

<?php
   $deque = new \Ds\Deque(['a', 'e', 'i', 'o', 'u']);
   echo "The original deque: \n";
   print_r($deque);
   #using reverse() function
   $deque->reverse();
   echo "The deque after reverse: \n";
   print_r($deque);
?>

Output

After executing the above program, it will produce the following output −

The original deque:
Ds\Deque Object
(
    [0] => a
    [1] => e
    [2] => i
    [3] => o
    [4] => u
)
The deque after reverse:
Ds\Deque Object
(
    [0] => u
    [1] => o
    [2] => i
    [3] => e
    [4] => a
)
Advertisements