Skip to content

Instantly share code, notes, and snippets.

@clohr
Last active March 4, 2017 19:15
Show Gist options
  • Select an option

  • Save clohr/80ce3448ad668d535eed2093c3d2873e to your computer and use it in GitHub Desktop.

Select an option

Save clohr/80ce3448ad668d535eed2093c3d2873e to your computer and use it in GitHub Desktop.
Creating a stack type in JS
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
}
}
}
@clohr
Copy link
Copy Markdown
Author

clohr commented Mar 4, 2017

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment