ds-algo-study
Version:
Just experimenting with publishing a package
56 lines (48 loc) • 1.04 kB
JavaScript
// ============================================================================
// Implementation Exercise: Stack
// ============================================================================
//
//
// Implement a Stack and all of its methods below!
class Node {
constructor(val) {
this.value = val;
this.next = null;
}
}
class Stack {
constructor() {
this.top = null;
this.bottom = null;
this.length = 0;
}
push(val) {
const newNode = new Node(val);
if (!this.length) {
this.bottom = newNode;
this.top = newNode;
} else {
newNode.next = this.top;
this.top = newNode;
}
this.length++;
return this.length;
}
pop() {
if (!this.length) return null;
const removedNode = this.top;
if (this.length === 1) {
this.top = null;
this.bottom = null;
} else {
this.top = this.top.next;
}
this.length--;
return removedNode.value;
}
size() {
return this.length;
}
}
exports.Node = Node;
exports.Stack = Stack;