interface Stack<T> {
readonly size: number;
push(value: T): void;
pop(): T;
}
type StackNode<T> = {
readonly value: T;
readonly next?: StackNode<T>;
};
class StackImpl<T> implements Stack<T> {
private _size = 0;
private head?: StackNode<T>;
get size() {
return this._size;
}
push(value: T): void {
const node = { value, next: this.head };
this.head = node;
this._size++;
}
pop(): T {
if (this.head == null) throw new Error('Stack is empty');
const node = this.head;
this.head = node.next;
this._size--;
return node.value;
}
}
const stack = new StackImpl<string>();
stack.push('j1');
stack.push('j2');
stack.push('j3');
stack.push('j4');
while (stack.size !== 0) console.log(stack.pop());