Introduction to the data structure stack (SplStack) of the php spl standard library

Source: Internet
Author: User
Tags spl

Introduction to the data structure stack (SplStack) of the php spl standard library

This article mainly introduces the data structure Stack (SplStack) of the php spl standard library. Stack is a special linear table, because it can only insert or delete elements at one end of a linear table (that is, to stack and to stack), you can refer

Stack is a special linear table, because it can only insert or delete elements at one end of the linear table (that is, the elements going into and out of the Stack)

SplStack inherits the double-stranded table (spldoublyshortlist) to implement the stack.

Class Abstract:

Simple use:

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

// Think of the stack as an inverted Array

$ Stack = new SplStack ();

/**

* The Difference Between Stack and double-stranded table is that IteratorMode has changed. The stack IteratorMode can only be:

* (1) spldoubly1_list: IT_MODE_LIFO | spldoubly1_list: IT_MODE_KEEP (default value: save data after iteration)

* (2) spldoubly1_list: IT_MODE_LIFO | spldoubly1_list: IT_MODE_DELETE (delete data after iteration)

*/

$ Stack-> setIteratorMode (spldoublyshortlist: IT_MODE_LIFO | spldoublyshortlist: IT_MODE_DELETE );

$ Stack-> push ('A ');

$ Stack-> push ('B ');

$ Stack-> push ('C ');

 

$ Stack-> pop (); // output stack

 

$ Stack-> offsetSet (0, 'First '); // If the index is 0, it is the last element.

 

Foreach ($ stack as $ item ){

Echo $ item. PHP_EOL; // first

}

 

Print_R ($ stack); // test IteratorMode

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.