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

打印 上一主题 下一主题

主题 1715|帖子 1715|积分 5145

1、题目

设计你的循环队列实现。 循环队列是一种线性数据布局,其操作体现基于 FIFO(先辈先出)原则而且队尾被毗连在队首之后以形成一个循环。它也被称为“环形缓冲器”。
循环队列的一个利益是我们可以利用这个队列之前用过的空间。在一个平常队列里,一旦一个队列满了,我们就不能插入下一个元素,即使在队列前面仍有空间。但是使用循环队列,我们能使用这些空间去存储新的值。
你的实现应该支持如下操作:


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

  1. MyCircularQueue circularQueue = new MyCircularQueue(3); // 设置长度为 3
  2. circularQueue.enQueue(1);  // 返回 true
  3. circularQueue.enQueue(2);  // 返回 true
  4. circularQueue.enQueue(3);  // 返回 true
  5. circularQueue.enQueue(4);  // 返回 false,队列已满
  6. circularQueue.Rear();  // 返回 3
  7. circularQueue.isFull();  // 返回 true
  8. circularQueue.deQueue();  // 返回 true
  9. circularQueue.enQueue(4);  // 返回 true
  10. 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、完整代码

  1. <!DOCTYPE html>
  2. <html lang="en">
  3.   <head>
  4.     <meta charset="UTF-8" />
  5.     <meta name="viewport" content="width=device-width, initial-scale=1.0" />
  6.     <title>设计循环队列</title>
  7.   </head>
  8.   <body>
  9.     <p>
  10.       设计你的循环队列实现。 循环队列是一种线性数据结构,其操作表现基于
  11.       FIFO(先进先出)原则并且队尾被连接在队首之后以形成一个循环。它也被称为“环形缓冲器”。
  12.     </p>
  13.     <p>
  14.       循环队列的一个好处是我们可以利用这个队列之前用过的空间。在一个普通队列里,一旦一个队列满了,我们就不能插入下一个元素,即使在队列前面仍有空间。但是使用循环队列,我们能使用这些空间去存储新的值。
  15.     </p>
  16.     <p>
  17.         <h4>你的实现应该支持如下操作:</h4>
  18.         MyCircularQueue(k): 构造器,设置队列长度为 k 。<br>
  19.         Front: 从队首获取元素。如果队列为空,返回 -1 。<br>
  20.         Rear: 获取队尾元素。如果队列为空,返回 -1 。<br>
  21.         enQueue(value): 向循环队列插入一个元素。如果成功插入则返回真。<br>
  22.         deQueue(): 从循环队列中删除一个元素。如果成功删除则返回真。<br>
  23.         isEmpty(): 检查循环队列是否为空。<br>
  24.         isFull(): 检查循环队列是否已满。<br>
  25.     </p>
  26.   </body>
  27.   <script>
  28.         /**
  29.     * @param {number} k
  30.     */
  31.     var MyCircularQueue = function(k) {
  32.         this.queue = new Array(k)
  33.         // 为了区分队列为空和队列中有元素,让头指针和尾指针指向-1
  34.         this.front = -1
  35.         this.rear = -1
  36.         // 当前元素数量
  37.         this.size = 0
  38.         // 队列最大容量
  39.         this.capacity = k
  40.     };
  41.     /**
  42.     * @param {number} value
  43.     * @return {boolean}
  44.     */
  45.     MyCircularQueue.prototype.enQueue = function(value) {
  46.         // 判断队列是否为空,如果为空,头指针需要向前移动
  47.         if (this.isEmpty()) {
  48.             this.front = 0
  49.         }
  50.         if (this.isFull()) {
  51.             console.log(false,this.queue);
  52.             return false
  53.         }
  54.         // 添加
  55.         // 尾指针向前
  56.         this.rear = (this.rear + 1)%this.capacity
  57.         // 添加
  58.         this.queue[this.rear] = value
  59.         this.size++
  60.         console.log(true,this.queue);
  61.         return true
  62.     };
  63.     /**
  64.     * @return {boolean}
  65.     */
  66.     MyCircularQueue.prototype.deQueue = function() {
  67.         //
  68.         if (this.isEmpty()) {
  69.             console.log('false',this.queue);
  70.             return false
  71.         }
  72.         if (this.front===this.rear) {
  73.             // 队列中只有一个元素且该元素即将被删除
  74.             this.front = -1
  75.             this.rear = -1
  76.         }else{
  77.             // 头指针前移,删除元素
  78.             this.front = (this.front+1)%this.capacity
  79.         }
  80.         this.size--;
  81.         console.log(true,this.queue);
  82.         return true
  83.     };
  84.     /**
  85.     * @return {number}
  86.     */
  87.     MyCircularQueue.prototype.Front = function() {
  88.         if (this.isEmpty()) {
  89.              console.log('-1',this.queue);
  90.             return -1
  91.         }
  92.         console.log(this.queue[this.front]);
  93.         return this.queue[this.front]
  94.     };
  95.     /**
  96.     * @return {number}
  97.     */
  98.     MyCircularQueue.prototype.Rear = function() {
  99.            if (this.isEmpty()) {
  100.             return -1
  101.         }
  102.         console.log(this.queue[(this.rear + this.capacity ) % this.capacity],this.queue);
  103.         // this.rear + this.capacity确保当 this.rear 为 0 时,计算出的索引不会是负数
  104.         return this.queue[(this.rear + this.capacity ) % this.capacity]
  105.     };
  106.     /**
  107.     * @return {boolean}
  108.     */
  109.     MyCircularQueue.prototype.isEmpty = function() {
  110.         if (this.size === 0) {
  111.             return true
  112.         }
  113.         return false
  114.     };
  115.     /**
  116.     * @return {boolean}
  117.     */
  118.     MyCircularQueue.prototype.isFull = function() {
  119.         if (this.size === this.capacity) {
  120.             return true
  121.         }
  122.         return false
  123.     };
  124.    
  125.    
  126.     var circularQueue = new MyCircularQueue(3); // 设置长度为 3
  127.     circularQueue.enQueue(1);  // 返回 true
  128.     circularQueue.enQueue(2);  // 返回 true
  129.     circularQueue.enQueue(3);  // 返回 true
  130.     circularQueue.enQueue(4);  // 返回 false,队列
  131.     circularQueue.Rear();  // 返回 3
  132.     circularQueue.isFull();  // 返回 true
  133.     circularQueue.deQueue();  // 返回 true
  134.     circularQueue.enQueue(4);  // 返回 true
  135.     circularQueue.Rear();  // 返回 4
  136.     /**
  137.     * Your MyCircularQueue object will be instantiated and called as such:
  138.     * var obj = new MyCircularQueue(k)
  139.     * var param_1 = obj.enQueue(value)
  140.     * var param_2 = obj.deQueue()
  141.     * var param_3 = obj.Front()
  142.     * var param_4 = obj.Rear()
  143.     * var param_5 = obj.isEmpty()
  144.     * var param_6 = obj.isFull()
  145.     */
  146.   </script>
  147. </html>
复制代码
6、力扣通过代码

  1.     var MyCircularQueue = function(k) {
  2.         this.queue = new Array(k)
  3.         // 为了区分队列为空和队列中有元素,让头指针和尾指针指向-1
  4.         this.front = -1
  5.         this.rear = -1
  6.         // 当前元素数量
  7.         this.size = 0
  8.         // 队列最大容量
  9.         this.capacity = k
  10.     };
  11.     /**
  12.     * @param {number} value
  13.     * @return {boolean}
  14.     */
  15.     MyCircularQueue.prototype.enQueue = function(value) {
  16.         // 判断队列是否为空,如果为空,头指针需要向前移动
  17.         if (this.isEmpty()) {
  18.             this.front = 0
  19.         }
  20.         if (this.isFull()) {
  21.             console.log(false,this.queue);
  22.             return false
  23.         }
  24.         // 添加
  25.         // 尾指针向前
  26.         this.rear = (this.rear + 1)%this.capacity
  27.         // 添加
  28.         this.queue[this.rear] = value
  29.         this.size++
  30.         console.log(true,this.queue);
  31.         return true
  32.     };
  33.     /**
  34.     * @return {boolean}
  35.     */
  36.     MyCircularQueue.prototype.deQueue = function() {
  37.         //
  38.         if (this.isEmpty()) {
  39.             console.log('false',this.queue);
  40.             return false
  41.         }
  42.         if (this.front===this.rear) {
  43.             // 队列中只有一个元素且该元素即将被删除
  44.             this.front = -1
  45.             this.rear = -1
  46.         }else{
  47.             // 头指针前移,删除元素
  48.             this.front = (this.front+1)%this.capacity
  49.         }
  50.         this.size--;
  51.         console.log(true,this.queue);
  52.         return true
  53.     };
  54.     /**
  55.     * @return {number}
  56.     */
  57.     MyCircularQueue.prototype.Front = function() {
  58.         if (this.isEmpty()) {
  59.              console.log('-1',this.queue);
  60.             return -1
  61.         }
  62.         console.log(this.queue[this.front]);
  63.         return this.queue[this.front]
  64.     };
  65.     /**
  66.     * @return {number}
  67.     */
  68.     MyCircularQueue.prototype.Rear = function() {
  69.            if (this.isEmpty()) {
  70.             return -1
  71.         }
  72.         console.log(this.queue[(this.rear + this.capacity ) % this.capacity],this.queue);
  73.         return this.queue[(this.rear + this.capacity) % this.capacity]
  74.     };
  75.     /**
  76.     * @return {boolean}
  77.     */
  78.     MyCircularQueue.prototype.isEmpty = function() {
  79.         if (this.size === 0) {
  80.             return true
  81.         }
  82.         return false
  83.     };
  84.     /**
  85.     * @return {boolean}
  86.     */
  87.     MyCircularQueue.prototype.isFull = function() {
  88.         if (this.size === this.capacity) {
  89.             return true
  90.         }
  91.         return false
  92.     };
  93.    
复制代码





免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!更多信息从访问主页:qidao123.com:ToB企服之家,中国第一个企服评测及商务社交产业平台。

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有账号?立即注册

x
回复

使用道具 举报

0 个回复

倒序浏览

快速回复

您需要登录后才可以回帖 登录 or 立即注册

本版积分规则

伤心客

论坛元老
这个人很懒什么都没写!
快速回复 返回顶部 返回列表