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