PHP data Structure Queue (Splqueue) and Priority queue (Splpriorityqueue) Simple use instances
This article mainly introduces the PHP data structure queue (SPLQUEUE) and the priority queue (Splpriorityqueue) Simple use instances, the need for friends can refer to the following
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:
The code is 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:
?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21st |
$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 /** * Set element out of Team mode * Splpriorityqueue::extr_data Extract only values * Splpriorityqueue::extr_priority only extract priority * 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 (); } |
http://www.bkjia.com/PHPjc/1000102.html www.bkjia.com true http://www.bkjia.com/PHPjc/1000102.html techarticle PHP Data Structure Queue (Splqueue) and Priority queue (Splpriorityqueue) Simple use examples This article mainly introduces the PHP data structure queue (SPLQUEUE) and the priority queue (Splpriorityqueue) Simple ...