PHP data Structure Queue (Splqueue) and Priority queue (Splpriorityqueue) Simple usage instance, priority queue data Structure _php tutorial

Source: Internet
Author: User

PHP data Structure Queue (Splqueue) and Priority queue (Splpriorityqueue) Simple usage instance, priority queue data structure


Queue this data structure is simpler, just like in our lives, it is characterized by FIFO.

The Splqueue class in PHP SPL is the implementation of queue operations, which, like the stack, can be easily implemented by inheriting a doubly linked list (spldoublylinkedlist).
The Splqueue class is summarized as follows:

Splqueue is simple to use as follows:

Copy the Code code as follows:
$queue = new Splqueue ();

/**
* The difference between the visible queue and the doubly linked list is that Iteratormode has changed, and the Iteratormode of the stack can only be:
* (1) spldoublylinkedlist::it_mode_fifo | Spldoublylinkedlist::it_mode_keep (default value, data save after iteration)
* (2) Spldoublylinkedlist::it_mode_fifo | Spldoublylinkedlist::it_mode_delete (data deduplication 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);

The priority queue Splpriorityqueue is implemented based on the heap (described below).
The Splpriorityqueue class summary is as follows:

Splpriorityqueue Simple to use:

$PQ = new Splpriorityqueue (); $PQ->insert (' A ', ten), $pq->insert (' B ', 1); $pq->insert (' C ', 8); echo $PQ->count (). Php_eol; 3echo $PQ->current (). Php_eol; A/** * Set element out of Team mode * Splpriorityqueue::extr_data extract only value * Splpriorityqueue::extr_priority extract priority only * Splpriorityqueue::extr_b OTH extract array contains values and precedence */$PQ->setextractflags (Splpriorityqueue::extr_data); while ($PQ->valid ()) {  Print_r ($PQ->current ()),//a c b  $pq->next ();}

http://www.bkjia.com/PHPjc/998573.html www.bkjia.com true http://www.bkjia.com/PHPjc/998573.html techarticle PHP data Structure Queue (Splqueue) and Priority queue (Splpriorityqueue) Simple use instance, priority queue data structure Queue This data structure is simpler, just like in the queue in our life, ...

  • 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.