-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathqueuePriority.js
More file actions
51 lines (44 loc) · 1.12 KB
/
queuePriority.js
File metadata and controls
51 lines (44 loc) · 1.12 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
// Priority Queue
const { createQueue } = require("./queue");
function createPriorityQueue() {
const lowPriorityQueue = createQueue();
const highPriorityQueue = createQueue();
return {
enqueue(item, isHighPriority = false) {
isHighPriority
? highPriorityQueue.enqueue(item)
: lowPriorityQueue.enqueue(item);
},
dequeue() {
if (!highPriorityQueue.isEmpty()) {
return highPriorityQueue.dequeue();
}
return lowPriorityQueue.dequeue();
},
peek() {
if (!highPriorityQueue.isEmpty()) {
return highPriorityQueue.peek();
}
return lowPriorityQueue.peek();
},
length() {
return highPriorityQueue.length + lowPriorityQueue.length;
},
isEmpty() {
return highPriorityQueue.isEmpty() && lowPriorityQueue.isEmpty();
}
};
}
const q = createPriorityQueue();
q.enqueue("highStuff", true);
q.enqueue("lowStuf");
q.enqueue("lowStuf2");
q.enqueue("lowStuf3");
q.enqueue("highStuff2", true);
console.log(q.peek());
q.dequeue();
console.log(q.peek());
q.dequeue();
console.log(q.peek());
q.dequeue();
console.log(q.peek());