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

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

PHPz
Release: 2018-10-12 14:52:24
Original
1156 people have browsed it

这篇文章主要介绍了PHP SPL标准库之数据结构栈(SplStack)介绍,栈(Stack)是一种特殊的线性表,因为它只能在线性表的一端进行插入或删除元素(即进栈和出栈),需要的朋友可以参考下

栈(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

 

更多相关教程请访问 php编程从入门到精通全套视频教程

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