其他分享
首页 > 其他分享> > 栈

作者:互联网

/**
 * 栈
*/
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 = {}
  }
}

标签:,count,return,res,else,isEmpty,items
来源: https://www.cnblogs.com/JianXin1994/p/16273518.html