-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path04-Queue.js
More file actions
53 lines (45 loc) · 1.32 KB
/
04-Queue.js
File metadata and controls
53 lines (45 loc) · 1.32 KB
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
// Create a queue
// Similar to Stacks except it follows First In First Out structure ---> FIFO
function Queue() {
// Initialize queue to an empty array
collection = [];
// Helper function to print the contents of the queue
this.print = function () {
console.log(collection);
};
// Enqueue method push the first item onto the queue
this.enqueue = function (element) {
// Calls array push method
collection.push(element);
};
// Dequeue method removes (pops) the first item off the queue
this.dequeue = function () {
// Calls array shift method
// Removes first item and returns it
return collection.shift();
};
// Front method returns the first item within the queue
// Does NOT remove the item
this.front = function () {
// Return the first element index
return collection[0];
};
// Size returns the length of the queue
this.size = function () {
// Use length method, pretty straight forward
return collection.length;
};
// isEmpty checks if the queue is empty
this.isEmpty = function () {
// Returns true/false depending on if the contents equal 0 (empty)
return collection.length === 0;
};
}
let queue = new Queue();
queue.enqueue("a");
queue.enqueue("b");
queue.enqueue("c");
queue.print();
queue.dequeue();
console.log(queue.front());
queue.print();