forked from trekhleb/javascript-algorithms
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathQueue.js
61 lines (54 loc) · 1.62 KB
/
Queue.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
import LinkedList from '../linked-list/LinkedList';
export default class Queue {
constructor() {
// We're going to implement Queue based on LinkedList since this
// structures a quite similar. Namely they both operates mostly with
// with theirs beginning and the end. Compare enqueue/de-queue
// operations of the Queue with append/prepend operations of LinkedList.
this.linkedList = new LinkedList();
}
/**
* @return {boolean}
*/
isEmpty() {
// The queue is empty in case if its linked list don't have tail.
return !this.linkedList.tail;
}
/**
* @return {*}
*/
peek() {
if (!this.linkedList.head) {
// If linked list is empty then there is nothing to peek from.
return null;
}
// Just read the value from the end of linked list without deleting it.
return this.linkedList.head.value;
}
/**
* @param {*} value
*/
enqueue(value) {
// Enqueueing means to stand in the line. Therefore let's just add
// new value at the beginning of the linked list. It will need to wait
// until all previous nodes will be processed.
this.linkedList.append(value);
}
/**
* @return {*}
*/
dequeue() {
// Let's try to delete the last node from linked list (the tail).
// If there is no tail in linked list (it is empty) just return null.
const removedHead = this.linkedList.deleteHead();
return removedHead ? removedHead.value : null;
}
/**
* @param [callback]
* @return {string}
*/
toString(callback) {
// Return string representation of the queue's linked list.
return this.linkedList.toString(callback);
}
}