Created
July 3, 2023 13:36
-
-
Save cjnghn/b2e55d1c21d34c27bc62c497e9e3f2f4 to your computer and use it in GitHub Desktop.
stack.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 Node { | |
constructor(item, next) { | |
this.item = item; | |
this.next = next; | |
} | |
} | |
class Stack { | |
constructor() { | |
this.top = null; | |
} | |
push(item) { | |
this.top = new Node(item, this.top); | |
} | |
pop() { | |
const item = this.top.item; | |
this.top = this.top.next; | |
return item; | |
} | |
} | |
function main() { | |
const stack = new Stack(); | |
stack.push(1); | |
stack.push(2); | |
stack.push(3); | |
console.log(stack.pop()); | |
console.log(stack.pop()); | |
console.log(stack.pop()); | |
} | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment