SPL,PHP 标准库(Standard PHP Library) ,从 PHP 5.0 起内置的组件和接口,且从 PHP5.3 已逐渐的成熟。SPL 在所有的 PHP5 开发环境中被内置,同时无需任何设置。
栈(Stack)原理图
SplStack
栈(Stack)是一种特殊的线性表,因为它只能在线性表的一端进行插入或删除元素(即进栈和出栈)。
栈是一种后进先出(LIFO)的数据结构。
PHP SPL中的SplStack继承自双向链表SplDoublyLinkedList。
参考资料
PHP-SPL-SplStack
SplStack类说明
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56
| SplStack extends SplDoublyLinkedList implements Iterator , ArrayAccess , Countable { __construct ( void )
void setIteratorMode ( int $mode ) public void SplDoublyLinkedList::add ( mixed $index , mixed $newval ) public mixed SplDoublyLinkedList::bottom ( void ) public int SplDoublyLinkedList::count ( void ) public mixed SplDoublyLinkedList::current ( void ) public int SplDoublyLinkedList::getIteratorMode ( void ) public bool SplDoublyLinkedList::isEmpty ( void ) public mixed SplDoublyLinkedList::key ( void ) public void SplDoublyLinkedList::next ( void ) public bool SplDoublyLinkedList::offsetExists ( mixed $index ) public mixed SplDoublyLinkedList::offsetGet ( mixed $index ) public void SplDoublyLinkedList::offsetSet ( mixed $index , mixed $newval ) public void SplDoublyLinkedList::offsetUnset ( mixed $index ) public mixed SplDoublyLinkedList::pop ( void ) public void SplDoublyLinkedList::prev ( void ) public void SplDoublyLinkedList::push ( mixed $value ) public void SplDoublyLinkedList::rewind ( void ) public string SplDoublyLinkedList::serialize ( void ) public void SplDoublyLinkedList::setIteratorMode ( int $mode ) public mixed SplDoublyLinkedList::shift ( void ) public mixed SplDoublyLinkedList::top ( void ) public void SplDoublyLinkedList::unserialize ( string $serialized ) public void SplDoublyLinkedList::unshift ( mixed $value ) public bool SplDoublyLinkedList::valid ( void ) }
|
SplStack使用
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39
| <?php $stack = new SplStack(); $stack->push(1); $stack->push(2); $stack->push(3);
echo 'bottom:'.$stack -> bottom().PHP_EOL; echo "top:".$stack->top().PHP_EOL;
$stack -> offsetSet(0, 10); echo "top:".$stack->top().'<br/>';
$stack -> rewind(); echo 'current:'.$stack->current().'<br/>';
$stack ->next(); echo 'current:'.$stack ->current().'<br/>';
$stack -> rewind(); while ($stack->valid()) { echo $stack->key().'=>'.$stack->current().PHP_EOL; $stack->next(); } echo '<br/>';
echo $stack->pop() .'--'; echo $stack->pop() .'--'; echo $stack->pop() .'--';
bottom:1 top:3 top:10 current:10 current:2 2=>10 1=>2 0=>1 10--2--1--
|