栈(Stack)

/**
 * 栈
*/
class stack {
  constructor() {
    this.count = 0
    this.items = {}
  }
  /**
   * 添加到栈顶
  */
  push(element) {
    this.items[this.count] = element
    this.count++
  }
  size() {
    return this.count
  }
  isEmpty() {
    return this.count === 0
  }
  /**
   * 弹出栈顶
  */
  pop() {
    if (this.isEmpty()) {
      return undefined
    } else {
      this.count--
      const res = this.items[this.count]
      delete this.items[this.count]
      return res
    }
  }
  /**
   * 查看队头
  */
  peek() {
    if (this.isEmpty()) {
      return undefined
    } else {
      return this.items[this.count - 1]
    }
  }
  clear() {
    this.count = 0
    this.items = {}
  }
}
————————

Stack

/**
 * 栈
*/
class stack {
  constructor() {
    this.count = 0
    this.items = {}
  }
  /**
   * 添加到栈顶
  */
  push(element) {
    this.items[this.count] = element
    this.count++
  }
  size() {
    return this.count
  }
  isEmpty() {
    return this.count === 0
  }
  /**
   * 弹出栈顶
  */
  pop() {
    if (this.isEmpty()) {
      return undefined
    } else {
      this.count--
      const res = this.items[this.count]
      delete this.items[this.count]
      return res
    }
  }
  /**
   * 查看队头
  */
  peek() {
    if (this.isEmpty()) {
      return undefined
    } else {
      return this.items[this.count - 1]
    }
  }
  clear() {
    this.count = 0
    this.items = {}
  }
}