forked from rubythonode/javascript-problems-and-solutions
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathqueue.js
47 lines (41 loc) · 730 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
/**
* Queue data structure.
*/
export default class Queue {
constructor() {
this.items = [];
}
/**
* Add new element to the queue
* @param {*} element
*/
enqueue(element) {
this.items.push(element);
}
/**
* Removed the first item in the queue
* @returns {*} The first item in the queue
*/
dequeue() {
return this.items.shift();
}
/**
* Returns the first item in the queue
* @returns {*} The first item in the queue
*/
first() {
return this.items[0];
}
/**
* Checks if the queue is empty
*/
isEmpty() {
return this.items.length === 0;
}
/**
* Returns the number of items in the queue
*/
size() {
return this.items.length;
}
}