Last active
February 28, 2021 01:23
-
-
Save osha7/de867c28737b6dbb1e6bba2841d96ee1 to your computer and use it in GitHub Desktop.
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() { | |
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