Forked from jesseyowell/gist:557eb12d957ac95854ff58a8fada8ca4
Last active
March 21, 2019 03:53
-
-
Save rstad/27bd3302e9ecc3ccbcc92f8cbfbee942 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