Skip to content

Instantly share code, notes, and snippets.

@brianxautumn
Created April 15, 2017 18:21
Show Gist options
  • Save brianxautumn/49de50f9eee886aaabff2d8c07d68954 to your computer and use it in GitHub Desktop.
Save brianxautumn/49de50f9eee886aaabff2d8c07d68954 to your computer and use it in GitHub Desktop.
Stack in JavaScript.js
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