Priority Queue (PHP) 
PHP's SplPriorityQueue class implements a max-heap. PHP also separately has SplHeap, SplMinHeap, and SplMaxHeap classes.

CODE:

$ph= new SplPriorityQueue;

$pq->insert('Clear drains',3);
$pq->insert('Feed cat',4);
$pq->insert('Make tea',5);
$pq->insert('Solve RC tasks',1);
$pq->insert('Tax return',2);

// This line causes extract() to return both the data and priority (in an associative array),
// Otherwise it would just return the data
$pq->setExtractFlags(SplPriorityQueue::EXTR_BOTH);

while (!$pq->isEmpty()) {
print_r($pq->extract());

}


OUTPUT:
Array
(
    [data] => Make tea
    [priority] => 5
)
Array
(
    [data] => Feed cat
    [priority] => 4
)
Array
(
    [data] => Clear drains
    [priority] => 3
)
Array
(
    [data] => Tax return
    [priority] => 2
)
Array
(
    [data] => Solve RC tasks
    [priority] => 1
)



See:Data Structures

[ view entry ] ( 1506 views )   |  print article

<<First <Back | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | Next> Last>>



2024 By Angel Cool