npm i gist:b22ca12fcee4e1d4c3c0f85d43080f92
Last active
May 30, 2021 09:29
-
-
Save rkatic/b22ca12fcee4e1d4c3c0f85d43080f92 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
This file contains hidden or 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
{ | |
"version": "0.0.3", | |
"name": "queue", | |
"main": "queue.js" | |
} |
This file contains hidden or 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
'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