Skip to content

Instantly share code, notes, and snippets.

@kgsnipes
Created January 22, 2024 15:00
Show Gist options
  • Save kgsnipes/f3cc1f44cef048572f7fb81501bc1f2d to your computer and use it in GitHub Desktop.
Save kgsnipes/f3cc1f44cef048572f7fb81501bc1f2d to your computer and use it in GitHub Desktop.
simple circular queue
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