SPL-栈

SplStack类通过使用一个双向链表来提供栈的主要功能。

SplStack 继承 SplDoublyLinkedList

方法

__construct — 构造使用双向链表实现的新堆栈
setIteratorMode — 设置迭代的模式

继承方法

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

push('a');
$stack->push('b');
$stack->push('c');

while ($stack->valid()) {
    echo $stack->key(), $stack->current(), PHP_EOL;
    $stack->next();
}

手册示例

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)
?>

你可能感兴趣的:(SPL-栈)