Skip to content

Instantly share code, notes, and snippets.

@Syncthetic
Created January 19, 2019 09:56
Show Gist options
  • Select an option

  • Save Syncthetic/5c1d2d7cec6b26fb83c3c35a261f715e to your computer and use it in GitHub Desktop.

Select an option

Save Syncthetic/5c1d2d7cec6b26fb83c3c35a261f715e to your computer and use it in GitHub Desktop.
Stack Implementation in JavaScript
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