/**
 * Implement the Stack with a given interface via array.
 *
 * @example
 * const stack = new Stack();
 *
 * stack.push(1); // adds the element to the stack
 * stack.peek(); // returns the peek, but doesn't delete it, returns 1
 * stack.pop(); // returns the top element from stack and deletes it, returns 1
 * stack.pop(); // undefined
 *
 */
class Stack {
  constructor() {
    this.top = null;
    this.size = 0;
  }

  push(element) {
    this.prev = this.top;
    this.top = element;
    this.size += 1;
  }

  pop() {
    const { top } = this;
    this.top = this.prev;
    this.size -= 1;
    return top;
  }

  peek() {
    return this.top;
  }
}

module.exports = Stack;
