1
0

Initial commit

This commit is contained in:
2025-12-16 20:39:11 +00:00
commit 8ae9352aa1
70 changed files with 3898 additions and 0 deletions

43
src/day1/Stack.ts Normal file
View File

@@ -0,0 +1,43 @@
type Node<T> = {
value: T;
prev?: Node<T>;
};
export default class Stack<T> {
public length: number;
private head?: Node<T>;
constructor() {
this.head = undefined;
this.length = 0;
}
push(item: T): void {
const node = { value: item } as Node<T>;
this.length++;
if (!this.head) {
this.head = node;
return;
}
node.prev = this.head;
this.head = node;
}
pop(): T | undefined {
this.length = Math.max(0, this.length - 1);
if (this.length === 0) {
const head = this.head;
this.head = undefined;
return head?.value;
}
const head = this.head as Node<T>;
this.head = head.prev;
return head?.value;
}
peek(): T | undefined {
return this.head?.value;
}
}