PHP如何使用Ds\Stack pop()函数?代码示例

2021年3月28日15:39:40 发表评论 799 次浏览

Ds\Stack::pop()PHP函数用于删除Stack实例顶部的元素。删除堆后, 此函数还返回堆栈的顶部元素。

语法如下:

mixed public Ds\Stack::pop ( void )

参数:该函数不接受任何参数。

返回值混合混合的此函数返回出现在堆栈顶部的元素, 并将其从堆栈中删除。

下面的程序说明了Ds \ Stack :: pop()PHP中的功能:

程序1:

<?php
  
// PHP program to illustarte the 
// pop() function 
  
// Create a Stack instance
$stack = new \Ds\Stack();
  
// Pushing elements to Stack
$stack ->push( "Welcome" );
$stack ->push( "to" );
$stack ->push( "GfG" );
  
// Print the initial Stack
print_r( $stack );
  
// Print the top element and remove it
print_r( $stack ->pop());
  
// Print the Stack again
print_r( $stack );
  
?>

输出如下:

Ds\Stack Object
(
    [0] => GfG
    [1] => to
    [2] => Welcome
)
GfG
Ds\Stack Object
(
    [0] => to
    [1] => Welcome
)

程式2:

<?php
  
// PHP program to illustarte the 
// pop() function 
  
// Create a Stack instance
$stack = new \Ds\Stack();
  
// Pushing Mixed value elements to Stack
$stack ->push( "Welcome" );
$stack ->push( "to" );
$stack ->push( "GfG" );
$stack ->push(10);
$stack ->push(5.5);
  
// Print the Stack initially
print_r( $stack );
  
// Print the top element and remove it
print_r( $stack ->pop());
  
// Print the stack again
print_r( $stack );
  
?>

输出如下:

Ds\Stack Object
(
    [0] => 5.5
    [1] => 10
    [2] => GfG
    [3] => to
    [4] => Welcome
)
5.5
Ds\Stack Object
(
    [0] => 10
    [1] => GfG
    [2] => to
    [3] => Welcome
)

参考:http://php.net/manual/en/ds-stack.pop.php


木子山

发表评论

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen: