Created
August 14, 2020 01:59
-
-
Save GAierken/7feaaab32f87a4bc363fbfa07d349e65 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 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