Created
April 21, 2021 15:25
-
-
Save RomanTurner/464d6fd83624df482672451d821df28b to your computer and use it in GitHub Desktop.
Queue in JS with array
This file contains 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 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