力扣每日一题0801-622. 设计循环队列

127 阅读3分钟

携手创作,共同成长!这是我参与「掘金日新计划 · 8 月更文挑战」的第5天,点击查看活动详情

设计你的循环队列实现。 循环队列是一种线性数据结构,其操作表现基于 FIFO(先进先出)原则并且队尾被连接在队首之后以形成一个循环。它也被称为“环形缓冲器”。

循环队列的一个好处是我们可以利用这个队列之前用过的空间。在一个普通队列里,一旦一个队列满了,我们就不能插入下一个元素,即使在队列前面仍有空间。但是使用循环队列,我们能使用这些空间去存储新的值。

你的实现应该支持如下操作:

  • MyCircularQueue(k): 构造器,设置队列长度为 k 。
  • Front: 从队首获取元素。如果队列为空,返回 -1 。
  • Rear: 获取队尾元素。如果队列为空,返回 -1 。
  • enQueue(value): 向循环队列插入一个元素。如果成功插入则返回真。
  • deQueue(): 从循环队列中删除一个元素。如果成功删除则返回真。
  • isEmpty(): 检查循环队列是否为空。
  • isFull(): 检查循环队列是否已满。

示例:

MyCircularQueue circularQueue = new MyCircularQueue(3); // 设置长度为 3
circularQueue.enQueue(1);  // 返回 true
circularQueue.enQueue(2);  // 返回 true
circularQueue.enQueue(3);  // 返回 true
circularQueue.enQueue(4);  // 返回 false,队列已满
circularQueue.Rear();  // 返回 3
circularQueue.isFull();  // 返回 true
circularQueue.deQueue();  // 返回 true
circularQueue.enQueue(4);  // 返回 true
circularQueue.Rear();  // 返回 4

数组

关于循环队列的概念可以参考:「循环队列」,我们可以通过一个数组进行模拟,通过操作数组的索引构建一个虚拟的首尾相连的环。在循环队列结构中,设置一个队尾 rear\textit{rear} 与队首 front\textit{front},且大小固定,结构如下图所示:

在循环队列中,当队列为空,可知 front=rear\textit{front}=\textit{rear};而当所有队列空间全占满时,也有 front=rear\textit{front}=\textit{rear}。为了区别这两种情况,假设队列使用的数组有 capacity\textit{capacity} 个存储空间,则此时规定循环队列最多只能有capacity1\textit{capacity} - 1 个队列元素,当循环队列中只剩下一个空存储单元时,则表示队列已满。根据以上可知,队列判空的条件是 front=rear\textit{front}=\textit{rear},而队列判满的条件是 front=(rear+1)modcapacity\textit{front} = (\textit{rear} + 1) \bmod \textit{capacity}。 对于一个固定大小的数组,只要知道队尾 rear\textit{rear} 与队首 front\textit{front},即可计算出队列当前的长度:

(rearfront+capacity)modcapacity(rear−front+capacity) \bmod capacity

循环队列的属性如下:

  • elements:一个固定大小的数组,用于保存循环队列的元素。
  • capacity:循环队列的容量,即队列中最多可以容纳的元素数量。
  • front:队列首元素对应的数组的索引。
  • rear:队列尾元素对应的索引的下一个索引。

循环队列的接口方法如下:

  • MyCircularQueue(int k): 初始化队列,同时base\textit{base} 数组的空间初始化大小为 k+1k + 1front,rear\textit{front}, \textit{rear} 全部初始化为 00
  • enQueue(int value):在队列的尾部插入一个元素,并同时将队尾的索引 rear\textit{rear} 更新为 (rear+1)modcapacity(\textit{rear} + 1) \bmod capacity
  • deQueue():从队首取出一个元素,并同时将队首的索引 front\textit{front} 更新为 (front+1)modcapacity(\textit{front} + 1) \bmod capacity
  • Front():返回队首的元素,需要检测队列是否为空。
  • Rear():返回队尾的元素,需要检测队列是否为空。
  • isEmpty():检测队列是否为空,根据之前的定义只需判断 rear\textit{rear} 是否等于 front\textit{front}
  • isFull():检测队列是否已满,根据之前的定义只需判断 front\textit{front} 是否等于 (rear+1)modcapacity(\textit{rear} + 1) \bmod \textit{capacity}
var MyCircularQueue = function(k) {
    this.capacity = k + 1;
    this.elements = new Array(this.capacity).fill(0);
    this.rear = 0;
    this.front = 0;
};

MyCircularQueue.prototype.enQueue = function(value) {
    if (this.isFull()) {
        return false;
    }
    this.elements[this.rear] = value;
    this.rear = (this.rear + 1) % this.capacity;
    return true;
};

MyCircularQueue.prototype.deQueue = function() {
    if (this.isEmpty()) {
        return false;
    }
    this.front = (this.front + 1) % this.capacity;
    return true;
};

MyCircularQueue.prototype.Front = function() {
    if (this.isEmpty()) {
        return -1;
    }
    return this.elements[this.front];
};

MyCircularQueue.prototype.Rear = function() {
    if (this.isEmpty()) {
        return -1;
    }
    return this.elements[(this.rear - 1 + this.capacity) % this.capacity];
};

MyCircularQueue.prototype.isEmpty = function() {
    return this.rear == this.front;
};

MyCircularQueue.prototype.isFull = function() {
    return ((this.rear + 1) % this.capacity) === this.front;
};