Skip to content

Instantly share code, notes, and snippets.

@GAierken
Created August 14, 2020 01:25
Show Gist options
  • Save GAierken/996cc92da264bd0f6330f1b4d61fb673 to your computer and use it in GitHub Desktop.
Save GAierken/996cc92da264bd0f6330f1b4d61fb673 to your computer and use it in GitHub Desktop.
class Node {
constructor(value){
this.value = value
this.next = null
}
}
class Stack {
constructor(){
this.first = null
this.last = null
this.size = 0
}
push(value){
let node = new Node(value)
if(!this.first){
this.first = node
this.last = node
}else{
let temp = this.first
this.first = node
this.first.next = temp
}
this.size++
return this.size
}
pop(){
if(!this.first) return null
let temp = this.first
if(this.first === this.last){
this.last = null
}
this.first = this.first.next
this.size--
return temp.value
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment