Home > Backend Development > PHP Tutorial > Introduction to the data structure stack (SplStack) of the PHP SPL standard library_PHP tutorial

Introduction to the data structure stack (SplStack) of the PHP SPL standard library_PHP tutorial

WBOY
Release: 2016-07-13 09:53:37
Original
1003 people have browsed it

Introduction to the data structure stack (SplStack) of the PHP SPL standard library

This article mainly introduces the introduction to the data structure stack (SplStack) of the PHP SPL standard library, stack (Stack) It is a special linear list because it can only insert or delete elements at one end of the linear list (i.e. push and pop). Friends in need can refer to

Stack is a special linear list because it can only insert or delete elements (i.e. push and pop) at one end of the linear list

SplStack is a stack that inherits SplDoublyLinkedList.

The class summary is as follows:

Simple usage is as follows:

 ?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

//把栈想象成一个颠倒的数组

$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

1 2

34 5 6 7 8 9
10
11
12 13 14 15 16 17 18 19 20 21
//Think of the stack as an upside-down array $stack = new SplStack(); /** * It can be seen that the difference between a stack and a double linked list is that the IteratorMode has changed. The IteratorMode of the stack can only be: * (1) SplDoublyLinkedList::IT_MODE_LIFO | SplDoublyLinkedList::IT_MODE_KEEP (default value, data is saved after iteration) * (2) SplDoublyLinkedList::IT_MODE_LIFO | SplDoublyLinkedList::IT_MODE_DELETE (data deleted after iteration) */ $stack->setIteratorMode(SplDoublyLinkedList::IT_MODE_LIFO | SplDoublyLinkedList::IT_MODE_DELETE); $stack->push('a'); $stack->push('b'); $stack->push('c'); $stack->pop(); //Pop from the stack $stack->offsetSet(0, 'first');//The index is 0 is the last element foreach($stack as $item) { echo $item . PHP_EOL; // first a } print_R($stack); //Test IteratorMode
http://www.bkjia.com/PHPjc/1000109.htmlwww.bkjia.comtruehttp: //www.bkjia.com/PHPjc/1000109.htmlTechArticleIntroduction to the data structure stack (SplStack) of the PHP SPL standard library. This article mainly introduces the data of the PHP SPL standard library. Introduction to structure stack (SplStack). Stack is a special linear table because it can only...
Related labels:
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