Ir para o conteúdo

CódigoNão tentada

Classe Stack genérica

Dificuldade: fácil

Construa Stack<T> com push, pop e peek (ambos T | undefined), um getter size e isEmpty(). Mantenha o armazenamento realmente privado (#field) e faça push aceitar somente T.

Ver solução de referência
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;
}
}
Carregando editor…