Skip to content

Instantly share code, notes, and snippets.

@GAierken
Created August 14, 2020 01:59
Show Gist options
  • Save GAierken/7feaaab32f87a4bc363fbfa07d349e65 to your computer and use it in GitHub Desktop.
Save GAierken/7feaaab32f87a4bc363fbfa07d349e65 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){
//create new node
let node = new Node(value)
// check if the first node is valid
if(!this.first){
//if not, make the new node the first and last node of the stack
this.first = node
this.last = node
}else{
// if there is at least one node in the stack
// make new node to be the first and the previous
// first node to be the next property of the new first
let temp = this.first
this.first = node
this.first.next = temp
}
// increment the size
this.size++
return this.size
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment