export default class Stack<T> {
  private items: T[];

  constructor() {
    this.items = [];
  }

  public push(element: T): void {
    this.items.push(element);
  }

  public pop(): T | undefined {
    return this.items.pop();
  }

  public peek(): T | undefined {
    return this.items[this.items.length - 1];
  }

  public isEmpty(): boolean {
    return this.items.length === 0;
  }

  public size(): number {
    return this.items.length;
  }

  public clear(): void {
    this.items = [];
  }

  public print(): void {
    console.log(this.items);
  }

  public toArray(): T[] {
    return [...this.items];
  }

  public clone(): Stack<T> {
    const newStack = new Stack<T>();
    newStack.items = [...this.items];
    return newStack;
  }
}
