-
Notifications
You must be signed in to change notification settings - Fork 0
/
PriorityQueue.php
93 lines (85 loc) · 1.97 KB
/
PriorityQueue.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
<?php
/*
* Author: doug@neverfear.org
*/
class PriorityList {
public $next;
public $data;
function __construct($data) {
$this->next = null;
$this->data = $data;
}
}
class PriorityQueue {
private $size;
private $liststart;
private $comparator;
function __construct($comparator) {
$this->size = 0;
$this->liststart = null;
$this->listend = null;
$this->comparator = $comparator;
}
function add($x) {
$this->size = $this->size + 1;
if($this->liststart == null) {
$this->liststart = new PriorityList($x);
} else {
$node = $this->liststart;
$comparator = $this->comparator;
$newnode = new PriorityList($x);
$lastnode = null;
$added = false;
while($node) {
if ($comparator($newnode, $node) < 0) {
// newnode has higher priority
$newnode->next = $node;
if ($lastnode == null) {
//print "last node is null\n";
$this->liststart = $newnode;
} else {
//print "Debug: " . $newnode->data . " has lower priority than " . $lastnode->data . "\n";
$lastnode->next = $newnode;
}
$added = true;
break;
}
$lastnode = $node;
$node = $node->next;
}
if (!$added) {
// Lowest priority - add to the very end
$lastnode->next = $newnode;
}
}
//print "Debug: Appended node. New size=" . $this->size . "\n";
//$this->debug();
}
function debug() {
$node = $this->liststart;
$i = 0;
if (!$node) {
print "<< No nodes >>\n";
return;
}
while($node) {
print "[$i]=" . $node->data[1] . " (" . $node->data[0] . ")\n";
$node = $node->next;
$i++;
}
}
function size() {
return $this->size;
}
function peak() {
return $this->liststart->data;
}
function remove() {
$x = $this->peak();
$this->size = $this->size - 1;
$this->liststart = $this->liststart->next;
//print "Debug: Removed node. New size=" . $this->size . "\n";
//$this->debug();
return $x;
}
}