Created
December 27, 2020 11:21
-
-
Save prashant1k99/1ecc900738977637f71eb5c5ddccf9e6 to your computer and use it in GitHub Desktop.
Stack implementation with Array
This file contains 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 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