Saltar al contenido

CódigoSin intentar

Clase genérica Stack

Dificultad: fácil

Construye Stack<T> con push, pop y peek (ambos T | undefined), un getter size e isEmpty(). Mantén el almacenamiento realmente privado (#field), y haz que push solo acepte T.

Ver solución de referencia
export class Stack<T> {
#items: T[] = []; // # makes it really private, so it doesn't show up on the instance
push(item: T): void {
this.#items.push(item);
}
pop(): T | undefined {
return this.#items.pop();
}
peek(): T | undefined {
return this.#items[this.#items.length - 1];
}
get size(): number {
return this.#items.length;
}
isEmpty(): boolean {
return this.#items.length === 0;
}
}
Cargando editor…