Skip to content

Instantly share code, notes, and snippets.

@rkatic
Last active May 30, 2021 09:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rkatic/b22ca12fcee4e1d4c3c0f85d43080f92 to your computer and use it in GitHub Desktop.
Save rkatic/b22ca12fcee4e1d4c3c0f85d43080f92 to your computer and use it in GitHub Desktop.

Queue

npm i gist:b22ca12fcee4e1d4c3c0f85d43080f92
{
"version": "0.0.3",
"name": "queue",
"main": "queue.js"
}
'use strict'
class Queue {
constructor () {
this._arr = []
this._i = 0
this.size = 0
}
push (value) {
this._arr.push(value)
++this.size
}
shift () {
if (this.size === 0) return undefined
const value = this._arr[this._i]
this._arr[this._i] = undefined
if (++this._i > --this.size) {
this._arr = this._arr.slice(this._i)
this._i = 0
}
return value
}
peek () {
return this._arr[this._i]
}
}
exports.Queue = Queue
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment