算法14(力扣622)设置循环队列

1、问题

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

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

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

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

2、示例

复制代码
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

3、理解题意

题意:利用数组实现循环队列的一下方法

4、具体步骤

(1)数组构建循环队列需要哪些元素?数组queue、头指针front、尾指针rear、capacity队列最大容量、size队列中当前元素数量

(2)判断队列是否为空,直接判断当前元素size是否为0

(3)判断队是否满,直接判断当前元素数size是否和队列的最大长度相同

(4)插入:

1)判断队满,满则返回。

2)判断队空?空,头指针前移:非空,尾指针前移,插入,当前元素数+1

(5)删除

1)判空?空,返回:非空(最后一个元素?是,重置头、尾指针:否,头指针前移,当前元素-1)

(6)从队首获取元素
1)判空?空,返回-1:非空,返回头指针指向的元素

(7)从队尾获取元素
1)空?空,返回-1:非空,返回尾指针指向的元素

5、完整代码

html 复制代码
<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>设计循环队列</title>
  </head>
  <body>
    <p>
      设计你的循环队列实现。 循环队列是一种线性数据结构,其操作表现基于
      FIFO(先进先出)原则并且队尾被连接在队首之后以形成一个循环。它也被称为"环形缓冲器"。
    </p>
    <p>
      循环队列的一个好处是我们可以利用这个队列之前用过的空间。在一个普通队列里,一旦一个队列满了,我们就不能插入下一个元素,即使在队列前面仍有空间。但是使用循环队列,我们能使用这些空间去存储新的值。
    </p>
    <p>
        <h4>你的实现应该支持如下操作:</h4>
        MyCircularQueue(k): 构造器,设置队列长度为 k 。<br>
        Front: 从队首获取元素。如果队列为空,返回 -1 。<br>
        Rear: 获取队尾元素。如果队列为空,返回 -1 。<br>
        enQueue(value): 向循环队列插入一个元素。如果成功插入则返回真。<br>
        deQueue(): 从循环队列中删除一个元素。如果成功删除则返回真。<br>
        isEmpty(): 检查循环队列是否为空。<br>
        isFull(): 检查循环队列是否已满。<br>
    </p>
  </body>
  <script>
        /**
    * @param {number} k
    */
    var MyCircularQueue = function(k) {
        this.queue = new Array(k) 
        // 为了区分队列为空和队列中有元素,让头指针和尾指针指向-1
        this.front = -1
        this.rear = -1
        // 当前元素数量
        this.size = 0 
        // 队列最大容量
        this.capacity = k
    };

    /** 
    * @param {number} value
    * @return {boolean}
    */
    MyCircularQueue.prototype.enQueue = function(value) {
        // 判断队列是否为空,如果为空,头指针需要向前移动
        if (this.isEmpty()) {
            this.front = 0
        }
        if (this.isFull()) {
            console.log(false,this.queue);
            return false
        }
        // 添加
        // 尾指针向前
        this.rear = (this.rear + 1)%this.capacity
        // 添加
        this.queue[this.rear] = value
        this.size++
        console.log(true,this.queue);
        return true
    };

    /**
    * @return {boolean}
    */
    MyCircularQueue.prototype.deQueue = function() {
        // 
        if (this.isEmpty()) {
            console.log('false',this.queue);
            return false
        }
        if (this.front===this.rear) {
            // 队列中只有一个元素且该元素即将被删除
            this.front = -1
            this.rear = -1
        }else{
            // 头指针前移,删除元素
            this.front = (this.front+1)%this.capacity
        }
        this.size--;
        console.log(true,this.queue);
        return true

    };

    /**
    * @return {number}
    */
    MyCircularQueue.prototype.Front = function() {
        if (this.isEmpty()) {
             console.log('-1',this.queue);
            return -1
        }
        console.log(this.queue[this.front]);
        return this.queue[this.front]
    };

    /**
    * @return {number}
    */
    MyCircularQueue.prototype.Rear = function() {
           if (this.isEmpty()) {
            return -1
        }
        console.log(this.queue[(this.rear + this.capacity ) % this.capacity],this.queue);
        // this.rear + this.capacity确保当 this.rear 为 0 时,计算出的索引不会是负数
        return this.queue[(this.rear + this.capacity ) % this.capacity]
    };

    /**
    * @return {boolean}
    */
    MyCircularQueue.prototype.isEmpty = function() {
        if (this.size === 0) {
            return true
        }
        return false
    };

    /**
    * @return {boolean}
    */
    MyCircularQueue.prototype.isFull = function() {
        if (this.size === this.capacity) {
            return true
        }
        return false
    };
   
   
    var 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
    /** 
    * Your MyCircularQueue object will be instantiated and called as such:
    * var obj = new MyCircularQueue(k)
    * var param_1 = obj.enQueue(value)
    * var param_2 = obj.deQueue()
    * var param_3 = obj.Front()
    * var param_4 = obj.Rear()
    * var param_5 = obj.isEmpty()
    * var param_6 = obj.isFull()
    */
  </script>
</html>

6、力扣通过代码

javascript 复制代码
    var MyCircularQueue = function(k) {
        this.queue = new Array(k) 
        // 为了区分队列为空和队列中有元素,让头指针和尾指针指向-1
        this.front = -1
        this.rear = -1
        // 当前元素数量
        this.size = 0 
        // 队列最大容量
        this.capacity = k
    };

    /** 
    * @param {number} value
    * @return {boolean}
    */
    MyCircularQueue.prototype.enQueue = function(value) {
        // 判断队列是否为空,如果为空,头指针需要向前移动
        if (this.isEmpty()) {
            this.front = 0
        }
        if (this.isFull()) {
            console.log(false,this.queue);
            return false
        }
        // 添加
        // 尾指针向前
        this.rear = (this.rear + 1)%this.capacity
        // 添加
        this.queue[this.rear] = value
        this.size++
        console.log(true,this.queue);
        return true
    };

    /**
    * @return {boolean}
    */
    MyCircularQueue.prototype.deQueue = function() {
        // 
        if (this.isEmpty()) {
            console.log('false',this.queue);
            return false
        }
        if (this.front===this.rear) {
            // 队列中只有一个元素且该元素即将被删除
            this.front = -1
            this.rear = -1
        }else{
            // 头指针前移,删除元素
            this.front = (this.front+1)%this.capacity
        }
        this.size--;
        console.log(true,this.queue);
        return true

    };

    /**
    * @return {number}
    */
    MyCircularQueue.prototype.Front = function() {
        if (this.isEmpty()) {
             console.log('-1',this.queue);
            return -1
        }
        console.log(this.queue[this.front]);
        return this.queue[this.front]
    };

    /**
    * @return {number}
    */
    MyCircularQueue.prototype.Rear = function() {
           if (this.isEmpty()) {
            return -1
        }
        console.log(this.queue[(this.rear + this.capacity ) % this.capacity],this.queue);
        return this.queue[(this.rear + this.capacity) % this.capacity]
    };

    /**
    * @return {boolean}
    */
    MyCircularQueue.prototype.isEmpty = function() {
        if (this.size === 0) {
            return true
        }
        return false
    };

    /**
    * @return {boolean}
    */
    MyCircularQueue.prototype.isFull = function() {
        if (this.size === this.capacity) {
            return true
        }
        return false
    };
   
相关推荐
W说编程9 分钟前
B树详解及其C语言实现
c语言·数据结构·b树·算法
orangapple18 分钟前
c# OpenCvSharp 16位转8位图
开发语言·算法·c#
艺杯羹34 分钟前
二级C语言题解:矩阵主、反对角线元素之和,二分法求方程根,处理字符串中 * 号
c语言·开发语言·数据结构·算法
vir021 小时前
P3654 First Step (ファーストステップ)(贪心算法)
数据结构·c++·算法
学徒小新1 小时前
(六)C++的函数模板与类模板
java·c++·算法
YYJ333_3331 小时前
蓝桥杯生命之树(DP)
职场和发展·蓝桥杯
丢掉幻想,坚持斗争2 小时前
蓝桥杯小白打卡第四天
算法·职场和发展·蓝桥杯
双人徐木子李2 小时前
后缀表达式(蓝桥杯19I)
职场和发展·蓝桥杯
qy发大财2 小时前
修剪二叉搜索树(力扣669)
数据结构·算法·leetcode·职场和发展
tamak2 小时前
c/c++蓝桥杯经典编程题100道(9)数组排序
c语言·数据结构·算法·蓝桥杯·排序算法