-
-
Notifications
You must be signed in to change notification settings - Fork 164
/
Copy pathqueue_array.js
62 lines (46 loc) · 1.33 KB
/
queue_array.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
class Queue
{
constructor()
{
this.items = []; // Array is used to implement a Queue
}
// List of main functions of queue data structure
enqueue(item) {
this.items.push(item); // adds an item to the queue
}
dequeue() {
// Underflow if queue is empty
if(this.isEmpty()) {
return "Underflow";
}
return this.items.shift(); // Removes an item from the front of a queue and return the removed item
}
front() {
if(this.isEmpty())
return "No elements in Queue";
return this.items[0]; // returns the front item of the queue without removing it.
}
// List of helper functions
isEmpty() {
return this.items.length === 0; // return true if the queue is empty.
}
printQueue() {
let data = "";
for(let i = 0; i < this.items.length; i++)
data += this.items[i] +" ";
return data;
}
}
function useQueue() {
let queue = new Queue();
console.log(queue.isEmpty()); // false
console.log(queue.dequeue()); // Underflow
queue.enqueue(1);
queue.enqueue(2);
queue.enqueue(3);
console.log(queue.printQueue()); // 1 2 3
console.log(queue.front()); // 1
console.log(queue.dequeue()); // 1
console.log(queue.printQueue()); // 2 3
}
useQueue();