Solución
solution.tsTypeScript
class Stack<T> {
private items: T[] = [];
push(item: T): void {
// Agrega el elemento al tope de la pila
this.items.push(item);
}
pop(): T | undefined {
// Elimina y retorna el elemento del tope; retorna undefined si la pila está vacía
return this.items.pop();
}
peek(): T {
// Retorna el elemento del tope sin eliminarlo
return this.items[this.items.length - 1];
}
get size(): number {
return this.items.length;
}
get isEmpty(): boolean {
return this.items.length === 0;
}
}
export function createStack(numbers: number[]): number | undefined {
// Crea un Stack, agrega todos los números y retorna el resultado de pop()
const stack = new Stack<number>();
for (const n of numbers) {
stack.push(n);
}
return stack.pop();
}0respuestas