PHP Data structure queue (SplQueue) and priority queue (SplPriorityQueue) simple use of instances, priority queue data structure _ PHP Tutorial

Source: Internet
Author: User
The PHP Data structure queue (SplQueue) and the priority queue (SplPriorityQueue) are simple to use instances, and the priority queue data structure. PHP Data structure queue (SplQueue) and priority queue (SplPriorityQueue) are simple to use instances. The data structure of priority queue data structure queue is simpler, just like queuing in our daily life, PHP Data structure queue (SplQueue) and priority queue (SplPriorityQueue) simple use of instances, priority queue data structure

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:

$ 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; // a/*** sets the element queuing mode * SplPriorityQueue: EXTR_DATA only extracts the value * SplPriorityQueue: EXTR_PRIORITY only extracts the priority * SplPriorityQueue :: EXTR_BOTH extract array containing value and priority */$ pq-> setExtractFlags (SplPriorityQueue: EXTR_DATA); while ($ pq-> valid ()) {print_r ($ pq-> current (); // a c B $ pq-> next ();}

Http://www.bkjia.com/PHPjc/998573.htmlwww.bkjia.comtruehttp://www.bkjia.com/PHPjc/998573.htmlTechArticlePHP data structure queue (SplQueue) and priority queue (SplPriorityQueue) simple use of instances, priority queue data structure queue this data structure is simpler, just like we live in the queue ,...

Related Article

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.