Last active
March 4, 2017 19:15
-
-
Save clohr/80ce3448ad668d535eed2093c3d2873e to your computer and use it in GitHub Desktop.
Creating a stack type in JS
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() { | |
| this._size = 0 | |
| this._storage = {} | |
| } | |
| getSize() { | |
| return this._size | |
| } | |
| getStorage() { | |
| return this._storage | |
| } | |
| push(data) { | |
| const size = ++this._size | |
| this._storage[size] = data | |
| } | |
| pop() { | |
| const size = this._size | |
| let deletedData | |
| if (size) { | |
| deletedData = this._storage[size] | |
| delete this._storage[size] | |
| this._size-- | |
| return deletedData | |
| } | |
| } | |
| } |
Author
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Inspired by https://code.tutsplus.com/series/data-structures-in-javascript--cms-772