Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

PHP - Ds Deque::reversed() Function



The PHP Ds\Deque::reversed() function is used to retrieve a reversed copy of the current deque. When the deque is reversed, it means changing the order of its elements, so the first element becomes the last, and the last becomes the first.

This function does not affect the original deque instead it returns a reversed copy of the original deque.

Syntax

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

public Ds\Deque Ds\Deque::reversed()

Parameters

This function does not accept any parameter.

Return value

This function returns the reversed copy of the deque.

Example 1

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

<?php
   $deque = new \Ds\Deque([1, 2, 3, 4]);
   echo "The orginal deque: \n";
   print_r($deque);
   echo "The reversed copy of a deque: \n";
   #using reversed() function
   print_r($deque->reversed());
?>

Output

The above program produces the following output −

The orginal deque:
Ds\Deque Object
(
    [0] => 1
    [1] => 2
    [2] => 3
    [3] => 4
)
The reversed copy of a deque:
Ds\Deque Object
(
    [0] => 4
    [1] => 3
    [2] => 2
    [3] => 1
)

Example 2

In the example below, we use thePHP Ds\Deque::reversed()function to retrieve a reversed copy of this deque (['x','y','z']) −

<?php
   $deque = new \Ds\Deque(['x','y','z']);
   echo "The original deque: \n";
   print_r($deque);
   echo "The reversed copy of the deque: \n";
   #using reversed() function
   print_r($deque->reversed());
?>

Output

Once the above program is executed, it will generate the following output −

The original deque:
Ds\Deque Object
(
    [0] => x
    [1] => y
    [2] => z
)
The reversed copy of the deque:
Ds\Deque Object
(
    [0] => z
    [1] => y
    [2] => x
)
Advertisements