-
Notifications
You must be signed in to change notification settings - Fork 153
/
Copy pathqueue.js
52 lines (45 loc) · 1.04 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
/**
* Implementation of queue usinig JS
* @author MadhavBahl
* @date 14/02/2019
*/
class Queue {
constructor (size) {
this.capacty = size;
this.data = [];
this.frontIndex = 0;
this.rearIndex = 0;
}
front () {
return data[this.frontIndex];
}
rear () {
return data[this.rearIndex];
}
enqueue (element) {
if (this.rearIndex >= this.capacty) {
console.log ("Overflow!");
return -1;
}
this.data.unshift (element);
console.log (element + ' added to the queue');
this.rearIndex++;
}
dequeue (element) {
if (this.rearIndex === 0) {
console.log ("Underflow!");
return -1;
}
console.log (this.data[this.rearIndex -1] + ' has been removed from the queue');
this.rearIndex--;
return this.data.pop ();
}
}
const myQ = new Queue (4);
myQ.dequeue ( );
myQ.enqueue (1);
myQ.enqueue (2);
myQ.enqueue (3);
myQ.enqueue (4);
myQ.enqueue (5);
myQ.dequeue ( );