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