qiuhongbingo / blog

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

队列的实现,先进先出 FIFO(First in、First out) #33

Open qiuhongbingo opened 4 years ago

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

  enqueue(...items) {
    return this.queue.push(...items)
  }

  dequeue() {
    return this.queue.shift()
  }

  front() {
    return this.isEmpty() ? undefined : this.queue[0]
  }

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

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

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