-
Notifications
You must be signed in to change notification settings - Fork 130
/
queue.js
79 lines (69 loc) · 1.32 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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
const DoublyLinkedList = require('./doubly_linked_list');
/**
* Class for Queues
*/
class Queue {
constructor(data) {
/** @private */
this._list = new DoublyLinkedList();
if (data !== undefined) {
this.push(data);
}
}
/**
* Get length of Queue
* @return {Number} Size of Queue
* @public
*/
get size() {
return this._list.length;
}
/**
* Check if Queue is empty or not
* @return {Boolean} `true` if empty else `false`
* @public
*/
isEmpty() {
return this._list.isEmpty();
}
/**
* Pushes node in Queue
* @param {*} data Data or value to be pushed
* @return {None}
* @public
*/
push(data) {
if (Array.isArray(data)) {
data.forEach(val => this._list.push(val));
} else {
this._list.push(data);
}
}
/**
* Pop node from Queue
* @return {*} Value of node which was popped
* @public
*/
pop() {
const front = this._list.tail;
this._list.deleteNode(front);
return front.value;
}
/**
* Get front value on Queue
* @return {*} Value of node at the front
* @public
*/
front() {
return this._list.tail.value;
}
/**
* Get back value on Queue
* @return {*} Value of node at the back
* @public
*/
back() {
return this._list.head.value;
}
}
module.exports = Queue;