Skip to content

Commit 5698318

Browse files
Merge pull request #157 from arryan231/patch-1
CircularQueue.js
2 parents 7e389c3 + 0c41be5 commit 5698318

File tree

1 file changed

+61
-0
lines changed

1 file changed

+61
-0
lines changed
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
class CircularQueue {
2+
constructor(size) {
3+
4+
this.queue = [];
5+
this.read = 0;
6+
this.write = 0;
7+
this.max = size - 1;
8+
this.count=0;
9+
10+
while (size > 0) {
11+
this.queue.push(null);
12+
size--;
13+
}
14+
}
15+
checkEmpty(){
16+
var count=0;
17+
for(let i=0;i<=this.max;i++){
18+
if(this.queue[i]==null){
19+
count++;
20+
}
21+
}
22+
if(count==this.max+1){
23+
return true;
24+
}
25+
return false;
26+
}
27+
checkFull(){
28+
var count=0;
29+
for(let i=0;i<=this.max;i++){
30+
if(this.queue[i]==null){
31+
count++;
32+
}
33+
}
34+
if(count>0) {
35+
return false;
36+
}
37+
return true;
38+
}
39+
40+
print() {
41+
return this.queue;
42+
}
43+
44+
enqueue(item) {
45+
if(!this.checkFull()){
46+
this.queue[this.write]=item;
47+
this.write=(this.write+1)%(this.max+1);
48+
}
49+
}
50+
51+
52+
dequeue() {
53+
if(!this.checkEmpty()){
54+
let val=this.queue[this.read];
55+
this.queue[this.read]=null;
56+
this.read=(this.read+1)%(this.max+1);
57+
return val;
58+
}
59+
else return null;
60+
}
61+
}

0 commit comments

Comments
 (0)