forked from knaxus/problem-solving-javascript
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
55 lines (46 loc) · 996 Bytes
/
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
const { LinkedList: SLL } = require('../LinkedList');
class Queue {
constructor() {
this.data = this.getStorage();
}
enqueue(element) {
this.data.enqueue(element);
}
dequeue() {
return this.data.dequeue();
}
peek() {
return this.data.peek();
}
length() {
return this.data.length();
}
destroy() {
return this.data.destroy();
}
// eslint-disable-next-line class-methods-use-this
getStorage() {
// encapsulating the internal implementation here
const storage = new SLL();
return {
enqueue(element) {
return storage.addAtEnd(element);
},
dequeue() {
const node = storage.removeFromBeginning();
return node ? node.data : node;
},
peek() {
const node = storage.getFirst();
return node ? node.data : node;
},
length() {
return storage.size;
},
destroy() {
storage.delete();
},
};
}
}
module.exports = Queue;