Skip to content

Instantly share code, notes, and snippets.

@RomanTurner
Created April 21, 2021 15:25
Show Gist options
  • Save RomanTurner/464d6fd83624df482672451d821df28b to your computer and use it in GitHub Desktop.
Save RomanTurner/464d6fd83624df482672451d821df28b to your computer and use it in GitHub Desktop.
Queue in JS with array
class Queue{
constructor(...items){
//initialize the items in queue
this._items = []
// enqueuing the items passed to the constructor
this.enqueue(...items)
}
enqueue(...items){
//push items into the queue
items.forEach( item => this._items.push(item) )
return this._items;
}
dequeue(count=1){
//pull out the first item from the queue
return this._items.splice(0,count);
}
peek(){
//peek at the first item from the queue
return this._items[0]
}
size(){
//get the length of queue
return this._items.length
}
isEmpty(){
//find whether the queue is empty or no
return this._items.length===0
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment