Repository navigation
Expand file tree
/
Copy pathqueue.js
More file actions
67 lines (65 loc) · 1.57 KB
/
Copy pathqueue.js
File metadata and controls
67 lines (65 loc) · 1.57 KB
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
let mQueue = function() {
this.collection = []
this.print = function() {
console.log(this.collection)
}
this.enqueue = function(value) {
return this.collection.push(value);
}
this.dequeue = function() {
return this.collection.shift()
}
this.front = function() {
return this.collection[0];
}
this.size = function() {
return this.collection.length;
}
this.isEmpty = function() {
return this.collection.length == 0
}
}
let myPriorityQueue = function() {
this.collection = []
this.print = function() {
console.log(this.collection)
}
this.enqueue = function(array) {
let added = false
if (this.isEmpty()) {
this.collection.push(array)
} else {
for (let i = 0; i < this.collection.length; i++) {
if (array[1] < this.collection[i][1]) {
this.collection.splice(i, 0, array)
added = true
break;
}
}
}
if (!added) {
this.collection.push(array)
}
}
this.dequeue = function() {
return this.collection.shift()
}
this.front = function() {
return this.collection[0];
}
this.size = function() {
return this.collection.length;
}
this.isEmpty = function() {
return this.collection.length == 0
}
}
let q = new myPriorityQueue;
q.enqueue([1, 2])
q.enqueue([2, 1])
q.enqueue([1, 3])
q.enqueue([3, 2])
q.enqueue([4, 4])
q.print()
q.dequeue()
q.print()