Last active
March 21, 2019 04:09
-
-
Save jesseyowell/557eb12d957ac95854ff58a8fada8ca4 to your computer and use it in GitHub Desktop.
custom pop method in a Stack
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.top = null; | |
} | |
push(value) { | |
var newNode = new Node(value); | |
newNode.next = this.top; | |
this.top = newNode; | |
} | |
pop() { | |
if(this.top === null) { | |
this.top = ''; | |
this.next = ''; | |
} | |
else { | |
this.next = this.top; | |
} | |
if(this.next === null) { | |
return null; | |
} | |
var temp = this.top; | |
if (this.top !== undefined && temp.value !== undefined) { | |
this.top = this.top.next; | |
return temp.value; | |
} | |
else return ''; | |
} | |
print() { | |
while (this.top !== null) { | |
console.log(this.pop()); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment