Skip to content

Instantly share code, notes, and snippets.

@jesseyowell
Last active March 21, 2019 04:09
Show Gist options
  • Save jesseyowell/557eb12d957ac95854ff58a8fada8ca4 to your computer and use it in GitHub Desktop.
Save jesseyowell/557eb12d957ac95854ff58a8fada8ca4 to your computer and use it in GitHub Desktop.
custom pop method in a Stack
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