Skip to content

Instantly share code, notes, and snippets.

@egnedko
Last active March 21, 2017 21:39
Show Gist options
  • Save egnedko/acd3a155cfdf1fde99e9247f503c534b to your computer and use it in GitHub Desktop.
Save egnedko/acd3a155cfdf1fde99e9247f503c534b to your computer and use it in GitHub Desktop.
Realisation Stack on js
class Stack extends Array {
constructor(STACKSIZE = Number.MAX_SAFE_INTEGER, ...arg) {
super(...arg);
Object.defineProperty(this, 'STACKSIZE', {
value: STACKSIZE
})
this.push = this.push.bind(this);
this.pop = this.pop.bind(this);
}
push(value) {
if(this.length === this.STACKSIZE) {
let error = new Error("Stack overflow\n");
error.name = 'Stack Error';
return error.toString();
}
return super.push(value);
}
pop() {
if(this.length === 0) {
let error = new Error("Stack underflow\n");
error.name = 'Stack 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