欢迎来到 跌倒的小黄瓜 的博客

♪(^∇^*)我要当大佬,(#^.^#)哈哈哈哈,(。-ω-)zzz我要成为优秀的人,(*^▽^*)٩(๑>◡<๑)۶O(∩_∩)O哈哈~~~~~~~~欢迎━(*`∀´*)ノ亻!

队列的实现



export default class MyCricularQueue {
  constructor (k) {
    // 保存数据长度为k的长度
    this.list = Array(k)
    this.front = 0
    this.rear = 0
    this.max = k
  }
  enQueue (v) {
    if (this.isFull()) {
      return false
    } else {
      this.list[this.rear] = v
      this.rear = (this.rear + 1) % this
      return true
    }
  }
  deQueue () {
    let v = this.list[this.front]
    this.list[this.front] = ''
    this.front = (this.front + 1) % this.max
    return v
  }
  isEmpty () {
    return this.front === this.rear && !this.list[this.front]
  }
  isFull () {
    return this.front === this.rear && this.list[this.front]
  }
  Front () {
    return this.list[this.front]
  }
  Rear () {
    let rear = this.rear - 1
    return this.list[rear < 0 ? this.max - 1 : rear]
  }
}

posted @ 2020-01-27 11:14  跌倒的小黄瓜  阅读(218)  评论(0编辑  收藏  举报