JSFiddle - React, Tailwind, and code Playground
by kpulkit29
JavaScript
class Node {
constructor(val) {
this.val = val;
this.next = null;
}
}
class Stack {
constructor() {
this.head = new Node(null);
}
push(element) {
let itr = this.head;
while(itr.next) {
itr = itr.next
}
itr.next = new Node(element)
}
top() {
let itr = this.head;
while(itr.next) {
itr = itr.next
}
return itr.val;
}
}
//////
const s = new Stack()
s.push(1)
/* s.push(2)
s.push(10) */
console.log(s.top());