-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
myQueue.js
43 lines (36 loc) · 1.06 KB
/
myQueue.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
import List from '../linked-list/myLinkedList.js';
export default class Queue {
constructor() {
// We're going to implement Queue based on LinkedList since the two
// structures are quite similar. Namely, they both operate mostly on
// the elements at the beginning and the end. Compare enqueue/dequeue
// operations of Queue with append/deleteHead operations of LinkedList.
this.linkedList = new List();
}
//isEmpty
isEmpty() {
return !this.linkedList.head;
}
//peek
peek() {
if (this.isEmpty()) {
return null;
}
return this.linkedList.head.value;
}
//enqueue
enqueue(value) {
this.linkedList.append(value);
return this;
}
//dequeue
dequeue() {
const removedHead = this.linkedList.deleteHead();
return removedHead ? removedHead.value : null;
}
//toString
toString(callback) {
// Return string representation of the queue's linked list.
return this.linkedList.toString(callback);
}
}