Created
April 15, 2017 18:21
-
-
Save brianxautumn/49de50f9eee886aaabff2d8c07d68954 to your computer and use it in GitHub Desktop.
Stack in JavaScript.js
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 Stack{ | |
constructor(){ | |
this.head = null; | |
} | |
push(data){ | |
var newNode = new Node(data); | |
newNode.next = this.head; | |
this.head = newNode; | |
} | |
pop(){ | |
var node = this.head; | |
if(this.head !== null ){ | |
this.head = this.head.next; | |
}else{ | |
return null; | |
} | |
return node.data; | |
} | |
peek(){ | |
if(this.head) | |
return this.head.data; | |
} | |
} | |
class Node{ | |
constructor(data){ | |
this.data = data; | |
this.next = null; | |
} | |
} | |
var stack = new Stack(); | |
stack.push(1); | |
stack.push(2); | |
stack.push(3); | |
console.log(stack.pop()); | |
console.log(stack.pop()); | |
console.log(stack.pop()); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment