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 ();
}