Skip to content

Instantly share code, notes, and snippets.

@prashant1k99
Created December 27, 2020 11:21
Show Gist options
  • Save prashant1k99/1ecc900738977637f71eb5c5ddccf9e6 to your computer and use it in GitHub Desktop.
Save prashant1k99/1ecc900738977637f71eb5c5ddccf9e6 to your computer and use it in GitHub Desktop.
Stack implementation with Array
class Stack {
constructor(val) {
if (!val) {
throw new Error('Please set the max for the stack')
}
this.max = val
this.__data__ = []
}
get __isFull() {
return this.__data__.length == (this.max - 1)
}
}
Stack.prototype.isEmpty = function () {
return this.__data__.length == 0
}
Stack.prototype.push = function (val) {
if (this.__isFull)
throw new Error('Overflow')
this.__data__.push(val)
}
Stack.prototype.pop = function () {
if (this.isEmpty)
throw new Error('Underflow')
return this.__data__.pop()
}
Stack.prototype.peek = function () {
return this.__data__[this.__data__.length]
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment