Created
January 19, 2019 09:56
-
-
Save Syncthetic/5c1d2d7cec6b26fb83c3c35a261f715e to your computer and use it in GitHub Desktop.
Stack Implementation in JavaScript
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 Stack { | |
| constructor(list) { | |
| this.list = {} | |
| this.count = 0 | |
| } | |
| push(value) { | |
| this.list[this.count] = value | |
| this.count++ | |
| } | |
| pop() { | |
| if ( this.count === 0) { return null } | |
| this.count-- | |
| const item = this.list[this.count] | |
| delete this.list[this.count] | |
| return item | |
| } | |
| peek() { | |
| return this.list[this.count - 1] | |
| } | |
| size() { | |
| return this.count | |
| } | |
| empty() { | |
| return this.count === 0 | |
| } | |
| swap() { | |
| const i = this.count - 1 | |
| let first = this.list[i] | |
| let second = this.list[i - 1] | |
| console.log(first, second) | |
| this.list[i] = second | |
| this.list[i - 1] = first | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment