Skip to content

Instantly share code, notes, and snippets.

@osha7
Last active February 28, 2021 01:23
Show Gist options
  • Save osha7/de867c28737b6dbb1e6bba2841d96ee1 to your computer and use it in GitHub Desktop.
Save osha7/de867c28737b6dbb1e6bba2841d96ee1 to your computer and use it in GitHub Desktop.
class Stack {
constructor() {
this.data = {}
this.size = 0
}
// add an element
push(element) {
this.size++
this.data[this.size] = element
}
// remove last element
pop(element) {
let removed = this.data[this.size]
delete this.data[this.size]
this.size--
return removed
}
// look at the last element
peek() {
return this.data[this.size]
}
// is the stack empty ?
isEmpty() {
return this.size === 0
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment