PHP data Structure Queue (Splqueue) and precedence queue (Splpriorityqueue) Simple use instance _php instance

Source: Internet
Author: User

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:

Copy Code code 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:

$PQ = new Splpriorityqueue ();
 
$PQ->insert (' A ', a);
$PQ->insert (' B ', 1);
$PQ->insert (' C ', 8);
 
echo $PQ->count (). Php_eol; 3
Echo $PQ->current (). Php_eol; A
 
/**
 * Set element out team mode
 * Splpriorityqueue::extr_data only extract value
 * splpriorityqueue::extr_priority priority only
 * Splpriorityqueue::extr_both extract array contains values and precedence
/$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.