-
Notifications
You must be signed in to change notification settings - Fork 270
/
Copy pathindex.js
66 lines (50 loc) · 1.04 KB
/
index.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
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
const { LinkedList: SinglyLinkedLists, Node } = require('../LinkedList');
class Queue extends SinglyLinkedLists {
constructor() {
super();
this.NotAllowed = 'Not Allowed';
}
enqueue(data) {
return this.addAtEnd(data);
}
dequeue() {
const node = this.removeFromBeginning();
return node ? node.data : node;
}
peek() {
const node = this.getFirst();
return node ? node.data : node;
}
length() {
return this.size;
}
destroy() {
this.delete();
}
/** Override and throw error for other LL methods */
addAtBeginning() {
throw new Error(this.NotAllowed);
}
addAt() {
throw new Error(this.NotAllowed);
}
removeFromEnd() {
throw new Error(this.NotAllowed);
}
getLast() {
throw new Error(this.NotAllowed);
}
getAt() {
throw new Error(this.NotAllowed);
}
removeAt() {
throw new Error(this.NotAllowed);
}
}
const q = new Queue();
q.enqueue(10);
q.enqueue(101);
q.enqueue(44);
console.log(q.length());
console.log(q.dequeue());
module.exports = Queue;