-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathqueue.js
More file actions
47 lines (36 loc) · 731 Bytes
/
queue.js
File metadata and controls
47 lines (36 loc) · 731 Bytes
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
class Node {
constructor(value, next) {
this.value = value;
this.next = next;
}
}
class Queue {
constructor() {
this.front = null;
this.back = null;
this.length = 0;
}
enqueue(value) {
let node = new Node(value);
if (!this.front) {
this.front = this.back = node;
} else {
this.back.next = node;
this.back = node;
}
return ++this.length;
}
dequeue() {
if (!this.front) return null;
const node = this.front;
if (this.front === this.back) this.front = this.back = null;
else this.front = this.front.next;
this.length--;
return node.value;
}
size() {
return this.length;
}
}
exports.Node = Node;
exports.Queue = Queue;