PHP - Ds Deque::shift() 函数



PHP 的 Ds\Deque::shift() 函数用于从当前双端队列中移除第一个元素,并返回被移除的第一个值。

Ds\Deque 类提供另一个名为 remove() 的函数,用于移除指定索引处的元素。如果索引为 0,则它会移除双端队列中的第一个元素。

语法

以下是 PHP Ds\Deque::shift() 函数的语法:

public mixed Ds\Deque::shift( void )

参数

此函数不接受任何参数。

返回值

此函数返回被移除的第一个值。

示例 1

以下是 PHP Ds\Deque::shift() 函数的基本示例:

<?php 
   $deque = new \Ds\Deque([10, 20, 30, 40, 50]); 
   echo "The deque elements are: \n";
   print_r($deque);
   #using shift() function
   echo "The removed element is: ";
   print_r($deque->shift());
?>

输出

上述程序输出如下:

The deque elements are:
Ds\Deque Object
(
    [0] => 10
    [1] => 20
    [2] => 30
    [3] => 40
    [4] => 50
)
The removed element is: 10

示例 2

以下是 PHP Ds\Deque::shift() 函数的另一个示例。我们使用此函数来移除此双端队列 (['a', 'e', 'i', 'o', 'u']) 中的第一个元素:

<?php 
   $deque = new \Ds\Deque(['a', 'e', 'i', 'o', 'u']); 
   echo "The deque elements are: \n";
   print_r($deque);
   #using shift() function
   echo "The removed element is: ";
   print_r($deque->shift());
   echo "\nThe deque after first element removed: \n";
   print_r($deque);
?>

输出

执行上述程序后,将显示以下输出:

The deque elements are:
Ds\Deque Object
(
    [0] => a
    [1] => e
    [2] => i
    [3] => o
    [4] => u
)
The removed element is: a
The deque after first element removed:
Ds\Deque Object
(
    [0] => e
    [1] => i
    [2] => o
    [3] => u
)

示例 3

在下面的示例中,我们使用 shift() 函数一次从双端队列 (["Tutorials", "Point", "Hyderabad", "India"]) 中移除前四个元素:

<?php 
   $deque = new \Ds\Deque (["Tutorials", "Point", "Hyderabad", "India"]); 
   echo "After first shift operation: ";
   print_r($deque -> shift());
   echo "\nAfter second shift operation: ";
   print_r($deque -> shift());
   echo "\nAfter third shift operation: ";
   print_r($deque -> shift());
   echo "\nAfter fourth shift operation: ";
   print_r($deque -> shift());
   echo "\nThe deque after removing all elements one by one: \n";
   print_r($deque);  
?>

输出

执行上述程序后,将产生以下输出:

After first shift operation: Tutorials
After second shift operation: Point
After third shift operation: Hyderabad
After fourth shift operation: India
The deque after removing all elements one by one:
Ds\Deque Object
(
)
广告