Home > Backend Development > PHP Tutorial > PHP SPL标准库之数据结构栈(SplStack)介绍_PHP

PHP SPL标准库之数据结构栈(SplStack)介绍_PHP

WBOY
Release: 2016-06-01 11:05:49
Original
920 people have browsed it

栈(Stack)是一种特殊的线性表,因为它只能在线性表的一端进行插入或删除元素(即进栈和出栈)

SplStack就是继承双链表(SplDoublyLinkedList)实现栈。

类摘要如下:

简单使用如下:

//把栈想象成一个颠倒的数组
$stack = new SplStack();
/**
 * 可见栈和双链表的区别就是IteratorMode改变了而已,栈的IteratorMode只能为:
 * (1)SplDoublyLinkedList::IT_MODE_LIFO | SplDoublyLinkedList::IT_MODE_KEEP (默认值,迭代后数据保存)
 * (2)SplDoublyLinkedList::IT_MODE_LIFO | SplDoublyLinkedList::IT_MODE_DELETE (迭代后数据删除)
 */
$stack->setIteratorMode(SplDoublyLinkedList::IT_MODE_LIFO | SplDoublyLinkedList::IT_MODE_DELETE);
$stack->push('a');
$stack->push('b');
$stack->push('c');
 
$stack->pop(); //出栈
 
$stack->offsetSet(0, 'first');//index 为0的是最后一个元素
 
foreach($stack as $item) {
 echo $item . PHP_EOL; // first a
}
 
print_R($stack); //测试IteratorMode
Copy after login


Related labels:
php
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template