PHP data Structure Queue (Splqueue) and Priority queue (Splpriorityqueue) simple usages

Source: Internet
Author: User

This article mainly introduced the PHP data structure queue (SPLQUEUE) and the priority queue (Splpriorityqueue) Simple use instance, needs the friend to be possible to refer to under

The data structure of queues is simpler, as in the line of our lives, its characteristics are FIFO.

PHP SPL Splqueue class is to implement the queue operation, and the same as the stack, it can inherit the double linked list (spldoublylinkedlist) easily implemented.

The Splqueue class summary is as follows:

The splqueue is simple to use as follows:

The code is as follows:

$queue = new Splqueue ();

/**

* Visible queue and double linked list is the difference is iteratormode changed, the stack of Iteratormode can only be:

* (1) spldoublylinkedlist::it_mode_fifo | Spldoublylinkedlist::it_mode_keep (default value, after iteration data save)

* (2) Spldoublylinkedlist::it_mode_fifo | Spldoublylinkedlist::it_mode_delete (Data deletion after iteration)

*/

$queue->setiteratormode (Spldoublylinkedlist::it_mode_fifo | Spldoublylinkedlist::it_mode_delete);

Splqueue::enqueue () is actually spldoublylinkedlist::p Ush ()

$queue->enqueue (' a ');

$queue->enqueue (' B ');

$queue->enqueue (' C ');

Splqueue::d equeue () is actually spldoublylinkedlist::shift ()

Print_r ($queue->dequeue ());

foreach ($queue as $item) {

Echo $item. Php_eol;

}

Print_r ($queue);

and the priority queue Splpriorityqueue is implemented based on the heap (after the article).

The Splpriorityqueue class summary is as follows:

Splpriorityqueue Simple to use:

?

1 2 3 4 5 6 7 8 9,

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.