数组模拟队列、循环队列、双向循环队列的JavaScript实现

队列

队列和栈一样,是一种操作受限制的线性表。只能查找首尾元素,不能查找中间元素。并且进出方式受限,栈是一种后进后出的单端(尾端)处理的数据结构。而队列是一种先进先出的双端处理的数据结构。

  • 用数组模拟队列的关键是按照操作规则
  • 每次添加元素添加到数组的尾部,删除元素则删除数组的头部元素
  • 队列的问题在于,元素出队列后,该元素所在的空间闲置
  • 这对于空间有限的计算机来说,无疑是一种浪费
  • 为了解决这个问题,设计了循环队列

循环队列

  • 为了实现空间的复用,关键是数组下标索引的循环利用
  • 首先设置 front 、rear 指针来表示数组下标索引
  • 为了解决循环问题,常用到的是 { % } 取模运算
  • % 取模运算,本质上是一个哈希运算,可以把任意数压缩到一定的空间,比如想知道一年中第122天是星期几,就可以用【121%7】,结果为二

在这里插入图片描述

  • 入队操作
    • 入队操作将新元素置于队尾(rear位置),并且rear指针后移一位
    • 然后通过取模操作确保索引下标始终在数组容量范围内
    • 特殊情况:当队列为空时入队front指针指向数组0下标
        MyCircularQueue.prototype.enQueue = function (value) {
            if (this.isFull()) {
                return false;
            }if(this.isEmpty()){
                this.front= 0;
            }    
            this.rear = (this.rear + 1) % this.maxSize;
            this.arr[this.rear] = value;
            return true;    
        };
  • 出队操作
    • 出队操作将front指针后移
    • 特殊情况:只有一个元素时,出队要将 front 和 rear 置为 -1
        MyCircularQueue.prototype.deQueue = function () {
            if(this.isEmpty()){
            return false;
            }
            if (this.front == this.rear) {
                this.front = this.rear = -1;
            } 
            else {
                this.front = (this.front + 1) % this.maxSize;
            }
            return true;
        };

完整代码:

        var MyCircularQueue = function (k) {
            this.front = -1;
            this.rear = -1;
            this.maxSize = k;
            this.arr = [];
        };
		//入队操作
        MyCircularQueue.prototype.enQueue = function (value) {
            if (this.isFull()) {
                return false;
            }if(this.isEmpty()){
                this.front= 0;
            }    
            this.rear = (this.rear + 1) % this.maxSize;
            this.arr[this.rear] = value;
            return true;    
        };
        //出队操作
        MyCircularQueue.prototype.deQueue = function () {
            if(this.isEmpty()){
            return false;
            }
            if (this.front == this.rear) {
                this.front = this.rear = -1;
            } 
            else {
                this.front = (this.front + 1) % this.maxSize;
            }
            return true;
        };
		//显示队首元素
        MyCircularQueue.prototype.Front = function () {
            if (this.isEmpty()) {
                return -1;
            } else {
                return this.arr[this.front];
            }
        };
		//显示队尾元素
        MyCircularQueue.prototype.Rear = function () {
            if (this.isEmpty()) {
                return -1;
            } else {
                return this.arr[this.rear];
            }
        };
		//判空
        MyCircularQueue.prototype.isEmpty = function () {
            return this.front == -1;
        };
		//判满
        MyCircularQueue.prototype.isFull = function () {
            return this.front == (this.rear + 1) % this.maxSize;
        };

双向循环队列

双向循环队列不再遵循 “先进先出” 的规则,而是按照头加,尾加、头减、尾减的具体规则来执行。
在这里插入图片描述

  • front == rear == -1
  • 头加:if(不满), 则front = (front + 1)% maxSize ,if(rear == -1),rear = 0
  • 尾加:if(!isFull), 则rear = (rear - 1 + maxSize) % maxSize ,if(front == -1),front = 0
  • 头减:front = (front - 1 + maxSize) % maxSize if(front == rear),front = rear = -1
  • 尾减:rear = (rear + 1) % maxSize ,if(front == rear),front = rear = -1
  • 判满:if(rear - 1 + maxSize == front)
  • 判空:front == -1

完整代码:

        var MyCircularDeque = function (k) {
            this.front = -1;
            this.rear = -1;
            this.maxSize = k;
            this.arr = new Array(k);
        };

        /**
         * Adds an item at the front of Deque. Return true if the operation is successful. 
         * @param {number} value
         * @return {boolean}
         */

        /*  */
        MyCircularDeque.prototype.insertFront = function (value) {
            if (this.isFull()) {
                return false;
            }
            if (this.isEmpty()) {
                this.rear = 0;
            }
            this.front = (this.front + 1) % this.maxSize;
            this.arr[this.front] = value;
            return true;
        };

        /**
         * Adds an item at the rear of Deque. Return true if the operation is successful. 
         * @param {number} value
         * @return {boolean}
         */

        /* 尾加:if(!isFull), 则rear = (rear - 1 + maxSize) % maxSize ,if(front == -1),front = 0 */
        MyCircularDeque.prototype.insertLast = function (value) {
            if (this.isFull()) {
                return false;
            }
            if (this.isEmpty()) {
                this.front = this.rear = this.maxSize - 1;
                this.arr[this.rear] = value;
                return true;
            }
            this.rear = (this.rear - 1 + this.maxSize) % this.maxSize;
            this.arr[this.rear] = value;
            return true;
        };

        /**
         * Deletes an item from the front of Deque. Return true if the operation is successful.
         * @return {boolean}
         */

        /* 头减:front = (front - 1 + maxSize) % maxSize if(front == rear),front = rear = -1 */
        MyCircularDeque.prototype.deleteFront = function () {
            if (this.isEmpty()) {
                return false;
            }
            if (this.front == this.rear) {
                this.front = this.rear = -1;
                return true;
            }
            this.front = (this.front - 1 + this.maxSize) % this.maxSize;
            return true;
        };

        /**
         * Deletes an item from the rear of Deque. Return true if the operation is successful.
         * @return {boolean}
         */

        /* 尾减:rear = (rear + 1) % maxSize ,if(front == rear),front = rear = -1*/
        MyCircularDeque.prototype.deleteLast = function () {
            if (this.isEmpty()) {
                return false;
            }
            if (this.front == this.rear) {
                this.front = this.rear = -1;
                return true;
            }
            this.rear = (this.rear + 1) % this.maxSize;
            return true;
        };

        /**
         * Get the front item from the deque.
         * @return {number}
         */

        /* 显示头:arr[front] */
        MyCircularDeque.prototype.getFront = function () {
            if (this.isEmpty()) {
                return -1;
            }
            return this.arr[this.front];
        };

        /**
         * Get the last item from the deque.
         * @return {number}
         */

        /* 显示尾:arr[rear] */
        MyCircularDeque.prototype.getRear = function () {
            if (this.isEmpty()) {
                return -1;
            }
            return this.arr[this.rear];
        };

        /**
         * Checks whether the circular deque is empty or not.
         * @return {boolean}
         */

        /* 判满:if(rear - 1 + maxSize == front)*/
        MyCircularDeque.prototype.isFull = function () {
            return (this.rear - 1 + this.maxSize)%this.maxSize == this.front;
        };

        /**
         * Checks whether the circular deque is full or not.
         * @return {boolean}
         */

        /* 判空:front  == -1 */
        MyCircularDeque.prototype.isEmpty = function () {
            return this.front  == -1;
        };
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值