php8.5
Home/ Manual/ ds / deque/ Ds\Deque::rotate

Ds\Deque::rotate

PHP function Edit on GitHub ✎

(PECL ds >= 1.0.0)

Rotates the deque by a given number of rotations

Description

Ds\Deque::rotate(int $rotations): void

Rotates the deque by a given number of rotations, which is equivalent to successively calling $deque->push($deque->shift()) if the number of rotations is positive, or $deque->unshift($deque->pop()) if negative.

Parameters

rotations

The number of times the deque should be rotated.

Return Values

Void. The deque of the current instance will be rotated.

Examples

Ds\Deque::rotate() example

php
<?php
$deque = new \Ds\Deque(["a", "b", "c", "d"]);

$deque->rotate(1);  // "a" is shifted, then pushed.
print_r($deque);

$deque->rotate(2);  // "b" and "c" are both shifted, then pushed.
print_r($deque);
?>

The above example will output something similar to:

output
(
    [0] => b
    [1] => c
    [2] => d
    [3] => a
)
Ds\Deque Object
(
    [0] => d
    [1] => a
    [2] => b
    [3] => c
)

Source: reference/ds/ds/deque/rotate.xml · from the official PHP manual (php/doc-en)