设计一个循环队列

本文详细介绍了循环队列的工作原理,并通过JavaScript实现了一个循环队列的数据结构,包括入队、出队、获取队首和队尾元素等操作。通过示例展示了循环队列在满和空状态下的状态变化,以及头尾指针如何更新。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

循环队列

循环队列相比于普通的队列,利用率更高。一个固定长度的队列,采用头指针和尾指针分别表示队列的开始和结束位置,通过入队出对操作控制指针。

下面演示出入队列时,head 和 tail 的位置变化

1.定义循环队列,长度5
   queue : ___  ___  ____  ____  ____
    / \
 head  tail
2.入队列(5)
   queue :   5  ___  ____  ____  ____
            / \
        head  tail
3.入队列(13)
   queue :   5  13   ____  ____  ____
            /     \
           head   tail
4.入队列(8)
   queue :   5  13   8  ____  ____
            /         \
          head          tail
5.入队列(2)
   queue :   5  13   8  2     ____
            /            \
        head              tail
6.入队列(10,队列已满)
   queue :   5  13   8  2     10
            /                   \
          head                  tail
7.出队列
   queue:  __  13   8  2     10
                /              \
              head              tail
8.出队列
   queue: __   __   8  2     10
                    /           \
                   head         tail
9.入队列(23)
   queue: 23   __   8  2     10
           /          \
          tail         head
10.入队列(6)
   queue: 23   6    8  2     10
               /      \
              tail    head

js 实现一个循环队列

/**
 * 构造函数
 * @param {number} k
 */
var MyCircularQueue = function(k) {
  this.queue = [];
  this.queue.length = k;
  this.head = 0; // 头
  this.tail = -1; // 尾
  this.length = 0;
};

/**
 * 插入队列
 * @param {number} value
 * @return {boolean}
 */
MyCircularQueue.prototype.enQueue = function(value) {
  if (this.length >= this.queue.length) return false;
  this.tail = this.tail === this.queue.length - 1 ? 0 : this.tail + 1; // 改变tail
  this.queue[this.tail] = value;
  this.length++;
  console.log(
    `queue=${this.queue}, head=${this.head}, tail=${this.tail}, length=${this.length}, `
  );
  if (this.length === this.queue.length) console.log("队列满了");
  console.log("-------");
  return true;
};

/**
 * 移出队列
 * @return {boolean}
 */
MyCircularQueue.prototype.deQueue = function() {
  if (this.length <= 0) return false;
  this.queue[this.head] = undefined;
  this.head = this.head === this.queue.length - 1 ? 0 : this.head + 1; // 改变head
  this.length--;
  console.log(
    `queue=${this.queue}, head=${this.head}, tail=${this.tail}, length=${this.length}, `
  );
  if (this.length === 0) console.log("队列已清空");
  console.log("-------");
  return true;
};

/**
 * 获取队首元素
 * @return {number}
 */
MyCircularQueue.prototype.Front = function() {
  if (this.length <= 0) return -1;
  return this.queue[this.head];
};

/**
 * 队尾元素
 * @return {number}
 */
MyCircularQueue.prototype.Rear = function() {
  if (this.length <= 0) return -1;
  return this.queue[this.tail];
};

/**
 * 检查循环队列是否为空
 * @return {boolean}
 */
MyCircularQueue.prototype.isEmpty = function() {
  console.log("isEmpty=", this.length === 0);
  return this.length === 0;
};

/**
 * 检查循环队列是否已满
 * @return {boolean}
 */
MyCircularQueue.prototype.isFull = function() {
  console.log("isFull=", this.length === this.queue.length);
  return this.length === this.queue.length;
};

测试

var obj = new MyCircularQueue(5);
var param_1 = obj.enQueue(5);
var param_11 = obj.enQueue(13);
var param_12 = obj.enQueue(8);
var param_13 = obj.enQueue(2);
var param_14 = obj.enQueue(10);
var param_2 = obj.deQueue();
var param_21 = obj.deQueue();
var param_3 = obj.enQueue(23);
var param_31 = obj.enQueue(6);
var param_21 = obj.deQueue();
var param_21 = obj.deQueue();
var param_21 = obj.deQueue();
var param_21 = obj.deQueue();
var param_21 = obj.deQueue();

// 输出结果
/**
 queue=5,,,,, head=0, tail=0, length=1, 
-------
queue=5,13,,,, head=0, tail=1, length=2, 
-------
queue=5,13,8,,, head=0, tail=2, length=3, 
-------
queue=5,13,8,2,, head=0, tail=3, length=4, 
-------
queue=5,13,8,2,10, head=0, tail=4, length=5, 
队列满了
-------
queue=,13,8,2,10, head=1, tail=4, length=4, 
-------
queue=,,8,2,10, head=2, tail=4, length=3, 
-------
queue=23,,8,2,10, head=2, tail=0, length=4, 
-------
queue=23,6,8,2,10, head=2, tail=1, length=5, 
队列满了
-------
queue=23,6,,2,10, head=3, tail=1, length=4, 
-------
queue=23,6,,,10, head=4, tail=1, length=3, 
-------
queue=23,6,,,, head=0, tail=1, length=2, 
-------
queue=,6,,,, head=1, tail=1, length=1, 
-------
queue=,,,,, head=2, tail=1, length=0, 
队列已清空
-------
*/

参考文献

leetcode-设计循环队列

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

wcc_chao@163.com

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值