Created
August 4, 2019 06:13
-
-
Save negarjf/4981a26db5d904f2bbce5891edd35e9a to your computer and use it in GitHub Desktop.
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.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