-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathPriorityQueue.js
40 lines (35 loc) · 992 Bytes
/
PriorityQueue.js
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
// Priority Queue Class
function PriorityQueue() {
this.collection = [];
this.printCollection = function () {
console.log(this.collection);
};
this.enqueue = function (e) {
if (this.collection.length === 0) {
this.collection[0] = e;
} else {
for (let i = 0; i < this.collection.length; i++) {
if (e[1] < this.collection[i][1]) {
this.collection.splice(i, 0, e);
return;
}
}
this.collection[this.collection.length] = e;
return;
}
};
this.dequeue = function () {
let item = this.collection[0][0];
this.collection.shift();
return item;
};
this.size = function () {
return this.collection.length;
};
this.front = function () {
return this.collection[0];
};
this.isEmpty = function () {
return this.collection.length === 0;
};
}