Skip to content

Instantly share code, notes, and snippets.

@negarjf
Created August 4, 2019 06:13
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save negarjf/4981a26db5d904f2bbce5891edd35e9a to your computer and use it in GitHub Desktop.
Save negarjf/4981a26db5d904f2bbce5891edd35e9a to your computer and use it in GitHub Desktop.
class Stack{
constructor(){
this.top = null;
this.size = 0;
}
get isEmpty(){
return this.top === null;
}
get peek(){
return this.top.data;
}
push(data){
const node = new Node(data);
node.next = this.top;
this.top = node;
this.size++;
}
pop(){
const data = this.top.data;
this.top = this.top.next;
this.size--;
return data;
}
}
class Node{
constructor(data){
this.data = data;
this.next = null;
}
}
module.exports = Stack;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment