forked from nas5w/javascript-patterns
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathqueue.js
56 lines (48 loc) · 957 Bytes
/
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
/**
* @description Queue Data Structure
*
*/
class Queue {
constructor() {
this.items = [];
}
/**
* @description adds an element to the queue
* @param {any} item
* @returns {void}
*/
enqueue(item) {
this.items.push(item);
}
/**
* @description removes an element from the queue
* @returns {any}
*/
dequeue() {
if (this.isEmpty()) throw new Error("Can't dequeue from an empty queue");
return this.items.shift();
}
/**
* @description returns the front element of the queue
* @returns {any}
*/
front() {
if (this.isEmpty()) throw new Error("Queue is empty");
return this.items[0];
}
/**
* @description checks if the queue is empty or not
* @returns {boolean}
*/
isEmpty() {
return this.items.length === 0;
}
/**
* @description gets the size of queue
* @returns {number}
*/
size() {
return this.items.length;
}
}
module.exports = Queue;