SplStack類通過使用一個雙向鏈表來提供棧的主要功能豌习。
SplStack 繼承 SplDoublyLinkedList
方法
__construct — 構(gòu)造使用雙向鏈表實現(xiàn)的新堆棧
setIteratorMode — 設(shè)置迭代的模式
繼承方法
add ( mixed $index , mixed $newval )
bottom ( void )
count ( void )
current ( void )
getIteratorMode ( void )
isEmpty ( void )
key ( void )
next ( void )
offsetExists ( mixed $index )
offsetGet ( mixed $index )
offsetSet ( mixed $index , mixed $newval )
offsetUnset ( mixed $index )
pop ( void )
prev ( void )
push ( mixed $value )
rewind ( void )
serialize ( void )
setIteratorMode ( int $mode )
shift ( void )
top ( void )
unserialize ( string $serialized )
unshift ( mixed $value )
valid ( void )
示例1
<?php
$stack = new SplStack();
$stack->push('a');
$stack->push('b');
$stack->push('c');
while ($stack->valid()) {
echo $stack->key(), $stack->current(), PHP_EOL;
$stack->next();
}
手冊示例
<?php
class Stack {
private $splstack;
function __construct(\SplStack $splstack)
{
$this->splstack = $splstack;
}
public function calculateSomme()
{
if ($this->splstack->count() > 1){
$val1 = $this->splstack->pop();
$val2 = $this->splstack->pop();
$val = $val1 + $val2;
$this->splstack->push($val);
$this->calculateSomme();
}
}
public function displaySomme()
{
$result = $this->splstack->pop();
return $result;
}
}
$splstack = new \SplStack();
$splstack->push(10);
$splstack->push(10);
$splstack->push(10);
$splstack->push(10);
$splstack->push(10);
$stack = new Stack($splstack);
$stack->calculateSomme();
die(var_dump($stack->displaySomme())); // int(50)
?>