Created
January 22, 2024 15:00
-
-
Save kgsnipes/f3cc1f44cef048572f7fb81501bc1f2d to your computer and use it in GitHub Desktop.
simple circular queue
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class CQ | |
{ | |
constructor(n) | |
{ | |
this.arr=new Array(n) | |
this.current=0 | |
} | |
add(data) | |
{ | |
let index=(this.current+1)%this.arr.length | |
this.arr[this.current]=data | |
this.current=index | |
} | |
remove(data) | |
{ | |
let index=this.arr.indexOf(data) | |
if(index!=-1) | |
{ | |
this.arr[index]=undefined | |
} | |
} | |
take() | |
{ | |
let data=this.arr[this.current] | |
this.arr[this.current]=undefined | |
this.current=(this.current+1)%this.arr.length | |
return data | |
} | |
display() | |
{ | |
console.log(this.arr) | |
} | |
} | |
let cq=new CQ(10) | |
for(i=1;i<12;i++) | |
{ | |
cq.add(i) | |
cq.display() | |
} | |
cq.remove(11) | |
cq.display() | |
cq.remove(10) | |
cq.display() | |
for(i=0;i<4;i++) | |
{ | |
console.log(cq.take()) | |
cq.display() | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment