Skip to content

Instantly share code, notes, and snippets.

@claeusdev
Created February 23, 2020 12:19
Show Gist options
  • Save claeusdev/83410778b0d093d9d4fdd58d760c6a0c to your computer and use it in GitHub Desktop.
Save claeusdev/83410778b0d093d9d4fdd58d760c6a0c to your computer and use it in GitHub Desktop.
Queue.prototype.enqueue = function(value){
if(this.size < this.capacity){
this.storage[this.tail++] = value
return this.size
}
return "Max capacity reached, remove one before you can add another."
}
Queue.prototype.dequeue = function(){
// get item from front of queue
// remove item from front
// if item at the fron is less than item at last increase item at head
// return item
const item = this.storage[this.head]
delete this.storage[this.head]
if(this.head < this.tail) this.head++;
return this.item
}
Queue.prototype.peek = function(){
// return item next in line to be removed
return this.storage[this.head]
}
Queue.prototype.size = function(){
// returns size of Queue
return this.tail - this.head
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment