blob: 76e513c3d646fb23d1e4c52d6c03c71677e024c6 (
plain)
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
|
export class QElement {
constructor(element, priority) {
this.element = element;
this.priority = priority;
}
}
export class PriorityQueue {
constructor(elt) {
this.items = [];
if (elt !== undefined) {
this.enqueue(elt, 0);
}
}
enqueue(element, priority) {
let qElement = new QElement(element, priority);
for (let i = 0; i < this.items.length; ++i) {
if (this.items[i].priority > qElement.priority) {
this.items.splice(i, 0, qElement);
return;
}
}
this.items.push(qElement);
}
dequeue() {
return this.items.pop(); // pop highest priority, use shift() for lower priority
}
front() {
return this.items[0];
}
rear() {
return this.items[this.items.length - 1];
}
isEmpty() {
return this.items.length === 0;
}
}
|