Skip to content

Instantly share code, notes, and snippets.

@islahh
Forked from deleteman/queue.js
Created September 27, 2020 04:31
Show Gist options
  • Save islahh/97aa8786921b0e49eee70950a0fd5077 to your computer and use it in GitHub Desktop.
Save islahh/97aa8786921b0e49eee70950a0fd5077 to your computer and use it in GitHub Desktop.
class Queue {
data = []
maxSize
constructor(initialData, maxSize = -1) {
this.data = Array.isArray(initialData) ? initialData : (typeof initialData == "undefined" ? [] : [initialData])
this.maxSize = maxSize
}
isFull() {
return this.maxSize != -1 ? (this.data.length == this.maxSize) : false
}
isEmpty() {
return this.data.length == 0
}
enqueue(item) {
if(this.isFull()) {
return false
}
this.data.push(item)
}
*generator() {
while(!this.isEmpty()) {
yield this.data.shift()
}
}
dequeue() {
const { value, done } = this.generator().next()
if(done) return false
return value
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment