PHP - Ds Deque::push() 函数



PHP 的 Ds\Deque::push() 函数用于将指定值追加到当前双端队列的末尾。此函数允许您同时推送单个或多个值,并且所有指定的值都会一个接一个地添加到双端队列的末尾。

您还可以将值推入一个双端队列([])。

语法

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

public void Ds\Deque::push([ mixed $...values ] )

参数

此函数接受一个名为“values”的单个参数,如下所述:

  • values - 需要追加的单个或多个值。

返回值

此函数不返回任何值。

示例 1

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

<?php
   $deque = new \Ds\Deque([10, 20, 30, 40, 50]);
   echo "The deque elements are: \n";
   print_r($deque);
   $val = 100;
   echo "The given value is: ".$val;
   #using push() function
   $deque->push($val);
   echo "\nThe deque after push: \n";
   print_r($deque);
?>

输出

上述程序输出以下内容:

The deque elements are:
Ds\Deque Object
(
    [0] => 10
    [1] => 20
    [2] => 30
    [3] => 40
    [4] => 50
)
The given value is: 100
The deque after push:
Ds\Deque Object
(
    [0] => 10
    [1] => 20
    [2] => 30
    [3] => 40
    [4] => 50
    [5] => 100
)

示例 2

以下是 PHP Ds\Deque::push() 函数的另一个示例。我们使用此函数在此双端队列([1, 2, 3, 4])中添加值56

<?php
   $deque = new \Ds\Deque([1, 2, 3, 4]);
   echo "The deque elements are: \n";
   print_r($deque);
   $val1 = 5;
   $val2 = 6;
   echo "The given values are: ".$val1.", ".$val2;
   #using the push() function
   $deque->push($val1);
   $deque->push($val2);
   echo "\nThe deque after push: \n";
   print_r($deque);
?>

输出

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

The deque elements are:
Ds\Deque Object
(
    [0] => 1
    [1] => 2
    [2] => 3
    [3] => 4
)
The given values are: 5, 6
The deque after push:
Ds\Deque Object
(
    [0] => 1
    [1] => 2
    [2] => 3
    [3] => 4
    [4] => 5
    [5] => 6
)

示例 3

空([])双端队列添加值。

在下面的示例中,我们使用push() 函数将元素22324252 添加到空双端队列([]),如下所示

<?php
   $deque = new \Ds\Deque([]);
   echo "The original deque: \n";
   print_r($deque);
   $deque->push(...[22, 32, 42, 52]);
   echo "The deque after push: \n";
   print_r($deque);
?>

输出

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

The original deque:
Ds\Deque Object
(
)
The deque after push:
Ds\Deque Object
(
    [0] => 22
    [1] => 32
    [2] => 42
    [3] => 52
)
广告