class Node {
constructor(data) {
this.data = data;
this.next = null;
}
}
class Stack {
constructor() {
this.top = null;
this.bottom = null;
this.length = 0;
}
push(value) {
const newNode = new Node(value);
if( this.length === 0) {
this.top = newNode;
this.bottom = newNode;
}
else {
newNode.next = this.top;
this.top = newNode;
}
this.length++;
}
peek() {
return this.top;
}
pop() {
if(this.length === 0) {
return null;
}
if(this.top === this.bottom) {
this.bottom = null;
}
const top = this.top;
this.top = top.next;
this.length--;
return top;
}
isEmpty() {
return (this.length === 0) ? true : false;
}
}
const stack = new Stack();
stack.push(1);
stack.push(2);
stack.push(3);
console.log(stack);
console.log(stack.pop());
console.log(stack.pop());
console.log(stack.peek());
console.log(stack.isEmpty());
console.log(stack.pop());
console.log(stack.isEmpty());
console.log(stack);
console.log(stack.pop());