-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathqueue.js
More file actions
85 lines (72 loc) · 1.89 KB
/
queue.js
File metadata and controls
85 lines (72 loc) · 1.89 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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
class Queue {
constructor() {
if (!Queue.instance) {
this.size = 5;
this.rearPointer = -1;
this.frontPointer = -1;
this.q = new Array();
this.ack = false;
Queue.instance = this;
}
return Queue.instance;
}
enQueue(msg) {
if ((this.rearPointer == this.size - 1 && this.frontPointer == 0) || (this.rearPointer == this.frontPointer - 1)) {
console.log("!!! Queue Overflow !!!");
} else if (this.frontPointer == -1) {
this.frontPointer = 0;
this.rearPointer = 0;
this.q[this.rearPointer] = msg;
} else if (this.rearPointer == this.size - 1 && this.frontPointer != 0) {
this.rearPointer = 0;
this.q[this.rearPointer] = msg;
} else {
this.rearPointer += 1;
// console.log(this.rearPointer);
this.q[this.rearPointer] = msg;
}
}
deQueue() {
if (this.frontPointer == -1) {
return "!!! Under Flow !!!";
}
let msg = this.q[this.frontPointer];
return msg;
}
display() {
console.log("Front pointer : " + this.frontPointer + ", Rear pointer : " + this.rearPointer);
console.log(this.q);
}
front() {
if (this.frontPointer !== -1)
return this.q[this.frontPointer];
console.log("Empty Queue");
}
ackMessage(msg) {
if (msg == "success" && this.frontPointer !== (-1)) {
this.q[this.frontPointer] = undefined;
if (this.frontPointer == this.rearPointer) {
this.frontPointer = -1;
this.rearPointer = -1;
} else if (this.frontPointer == this.size - 1) {
this.frontPointer = 0;
} else {
this.frontPointer += 1;
}
//this.ack = true;
}
//this.ack = false;
}
rear() {
if (this.rearPointer !== -1)
return this.q[this.rearPointer];
console.log("Empty Queue");
}
size() {
return this.size;
}
}
const queue = new Queue();
module.exports = {
queue: queue
}