Simple use of PHP Data Structure Queue (SplQueue) and priority queue (SplPriorityQueue) Instances

Source: Internet
Author: User

Simple use of PHP Data Structure Queue (SplQueue) and priority queue (SplPriorityQueue) Instances

This article mainly introduces simple Use Cases of PHP Data Structure Queue (SplQueue) and priority queue (SplPriorityQueue). For more information, see

The data structure of queue is simpler, just like queuing in our daily life. It features FIFO ).

In php spl, The SplQueue class implements queue operations. Like the stack, it can also inherit the double-stranded table (spldoubly1_list) for easy implementation.

The summary of the SplQueue class is as follows:

SplQueue is easy to use as follows:

The Code is as follows:

$ Queue = new SplQueue ();

/**

* The difference between a queue and a double-stranded table is that the IteratorMode is changed. The stack IteratorMode can only be:

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

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

*/

$ Queue-> setIteratorMode (spldoubly1_list: IT_MODE_FIFO | spldoubly1_list: IT_MODE_DELETE );

// SplQueue: enqueue () is actually the spldoubly1_list: push ()

$ Queue-> enqueue ('A ');

$ Queue-> enqueue ('B ');

$ Queue-> enqueue ('C ');

// SplQueue: dequeue () is actually the spldoubly1_list: shift ()

Print_r ($ queue-> dequeue ());

Foreach ($ queue as $ item ){

Echo $ item. PHP_EOL;

}

Print_r ($ queue );

The priority queue SplPriorityQueue is implemented based on the heap (introduced later.

The class Summary of SplPriorityQueue is as follows:

Simple use of SplPriorityQueue:

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

$ Pq = new SplPriorityQueue ();

 

$ Pq-> insert ('A', 10 );

$ Pq-> insert ('B', 1 );

$ Pq-> insert ('C', 8 );

 

Echo $ pq-> count (). PHP_EOL; // 3

Echo $ pq-> current (). PHP_EOL; //

 

/**

* Set the element team-out mode.

* SplPriorityQueue: EXTR_DATA only extracts values.

* SplPriorityQueue: EXTR_PRIORITY only extracts priority

* SplPriorityQueue: EXTR_BOTH extracts the array containing values and priorities.

*/

$ Pq-> setExtractFlags (SplPriorityQueue: EXTR_DATA );

 

While ($ pq-> valid ()){

Print_r ($ pq-> current (); // a c B

$ Pq-> next ();

}

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.