-
Notifications
You must be signed in to change notification settings - Fork 1
/
queue.js
67 lines (58 loc) · 1.02 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
/**
* Represents a Queue data structure.
*/
class Queue{
constructor(){
this.items = [];
}
enqueue(item){
this.items.push(item);
}
size(){
return this.items.length;
}
isEmpty(){
return this.items.length == 0;
}
dequeue(){
if(this.isEmpty()){
return "Underflow";
}
return this.items.shift();
}
front(){
if(this.isEmpty()){
return "No elements in Queue"
}
return this.items[0]
}
display(){
return this.items
}
}
q = new Queue();
q.enqueue(1);
q.enqueue(2);
q.enqueue(3);
q.enqueue(4);
console.log(q.size())
console.log(q.display())
console.log(q.front())
q.dequeue()
q.enqueue(5);
q.enqueue(6);
q.enqueue(7);
console.log(q.size())
q.dequeue()
console.log(q.size())
q.dequeue()
console.log(q.size())
q.dequeue()
console.log(q.size())
q.dequeue()
console.log(q.size())
q.dequeue()
console.log(q.size())
q.dequeue()
console.log(q.size())
console.log(q.dequeue())