Skip to content

Instantly share code, notes, and snippets.

@cjnghn
Created July 3, 2023 13:36
Show Gist options
  • Save cjnghn/b2e55d1c21d34c27bc62c497e9e3f2f4 to your computer and use it in GitHub Desktop.
Save cjnghn/b2e55d1c21d34c27bc62c497e9e3f2f4 to your computer and use it in GitHub Desktop.
stack.js
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