Skip to content

Instantly share code, notes, and snippets.

@egnedko
Created November 1, 2016 15:23
Show Gist options
  • Save egnedko/f40f12e71e56a24257626b4db2f74d59 to your computer and use it in GitHub Desktop.
Save egnedko/f40f12e71e56a24257626b4db2f74d59 to your computer and use it in GitHub Desktop.
Realisation Queue on js
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