Created
November 1, 2016 15:23
-
-
Save egnedko/f40f12e71e56a24257626b4db2f74d59 to your computer and use it in GitHub Desktop.
Realisation Queue on 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
class Queue extends Array { | |
constructor (STACKSIZE = Number.MAX_SAFE_INTEGER, ...arg) { | |
super(...arg) | |
Object.defineProperty(this, 'STACKSIZE', { | |
value: STACKSIZE | |
}) | |
this.enqueue = this.enqueue.bins(this); | |
this.dequeue = this.dequeue.bind(this); | |
} | |
enqueue() { | |
if(this.length >= this.STACKSIZE) { | |
let error = new Error("Queue overflow\n"); | |
error.name = 'Queue Error'; | |
return error.toString(); | |
} | |
return super.unshift(item); | |
} | |
dequeue() { | |
if(this.length <= 0) { | |
let error = new Error("Queue underflow\n"); | |
error.name = 'Queue Error'; | |
return error.toString(); | |
} | |
return this.splice(-1,1); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment