qiuhongbingo / blog

Writing something in Issues.
https://github.com/qiuhongbingo/blog/issues
3 stars 0 forks source link

栈的实现,后进先出 LIFO(Last in、First out) #32

Open qiuhongbingo opened 4 years ago

qiuhongbingo commented 4 years ago
class Stack {
  constructor(...args) {
    this.stack = [...args]
  }

  push(...items) {
    return this.stack.push(...items)
  }

  pop() {
    return this.stack.pop()
  }

  peek() {
    return this.isEmpty() ? undefined : this.stack[this.size() - 1]
  }

  isEmpty() {
    return this.size() == 0
  }

  size() {
    return this.stack.length
  }
}