代码实例
<?php
namespace app\index\controller;
use SplStack;
class Index
{
public function index()
{
$stack = new SplStack;
$stack -> push('a');
$stack -> push('b');
$stack -> push('c');
dump($stack);
echo "Bottom:".$stack->bottom()."<br/>";
echo "Top:".$stack->top();
$stack -> offsetSet(1,'C');
dump($stack);
$stack->rewind();
echo 'current:'.$stack->current()."<br/>";
$stack->next();
echo 'current:'.$stack->current()."<br/>";
$stack->rewind();
while ($stack -> valid()) {
echo $stack -> key()."=>".$stack->current()."<br/>";
$stack -> next();
}
$popObj = $stack->pop();
echo "Poped object:".$popObj."<br/>";
dump($stack);
}
}
运行结果
object(SplStack)#4 (2) {
["flags":"SplDoublyLinkedList":private] => int(6)
["dllist":"SplDoublyLinkedList":private] => array(3) {
[0] => string(1) "a"
[1] => string(1) "b"
[2] => string(1) "c"
}
}
Bottom:a
Top:c
object(SplStack)#4 (2) {
["flags":"SplDoublyLinkedList":private] => int(6)
["dllist":"SplDoublyLinkedList":private] => array(3) {
[0] => string(1) "a"
[1] => string(1) "C"
[2] => string(1) "c"
}
}
current:c
current:C
2=>c
1=>C
0=>a
Poped object:c
object(SplStack)#4 (2) {
["flags":"SplDoublyLinkedList":private] => int(6)
["dllist":"SplDoublyLinkedList":private] => array(2) {
[0] => string(1) "a"
[1] => string(1) "C"
}
}