• 使用javaScript实现一个双端队列


    1.双端队列,一种允许我们从队列的前面和后面添加和删除元素的队列。

    2.创建一个双端队列的类

    class Deque{
        constructor(){
            this.lowestCount = 0;
            this.count = 0;
            this.items = {};
        }
    }

    3.判断队列是否为空

        isEmpty(){
            return this.size() === 0;
        }

    4.获取队列的长度

        size(){
            return this.count - this.lowestCount;
        }

    5.从队列头部添加元素

        addFront(element){
            if(this.size()==0){
                addBack(element);
            }else if(this.lowestCount>0){
                this.lowestCount--;
                this.items[this.lowestCount] = element;
            }else{
                for(let i = this.count; i > 0; i++){
                    this.items[i] = this.items[i - 1];
                }
                this.count++;
                this.lowestCount = 0;
                this.items[0] = element;
            }
        }

     6.从队列尾部添加元素

        addBack(element){
            this.items[this.count] = element;
            this.count++;
        }

    7.查看队列头部元素

        peekFront(){
            return this.items[this.lowestCount];
        }

    8.查看队列尾部元素

        peekBack(){
            return this.items[this.count-1];
        }

    9.移除队列头部元素

        removeFront(){
            if(this.isEmpty()) return 'queue is null';
            let result = this.items[this.lowestCount];
            delete this.items[this.lowestCount];
            this.lowestCount++;
            return result;
        }

    10.移除队列尾部元素

        removeBack(){
            if(this.isEmpty())return "deque is null";
            this.count--;
            let result = this.items[this.count];
            delete this.items[this.count];
            return result;
        }

    11.打印队列的所有元素

        toString(){
            if(this.isEmpty())return 'deque is null';
            let objString = `${this.items[this.lowestCount]}`;
            for(let i = this.lowestCount+1;i < this.count; i++){
                objString = `${objString},${this.items[i]}`;
            }
            return objString;
        }

    12.清除队列的元素

        clear(){
            this.count = 0;
            this.lowestCount = 0;
            this.items = {};
        }

    13.完整代码

    class Deque{
        constructor(){
            this.lowestCount = 0;
            this.count = 0;
            this.items = {};
        }
        isEmpty(){
            return this.size() === 0;
        }
        size(){
            return this.count - this.lowestCount;
        }
        addFront(element){
            if(this.size()==0){
                addBack(element);
            }else if(this.lowestCount>0){
                this.lowestCount--;
                this.items[this.lowestCount] = element;
            }else{
                for(let i = this.count; i > 0; i++){
                    this.items[i] = this.items[i - 1];
                }
                this.count++;
                this.lowestCount = 0;
                this.items[0] = element;
            }
        }
        addBack(element){
            this.items[this.count] = element;
            this.count++;
        }
        peekFront(){
            return this.items[this.lowestCount];
        }
        peekBack(){
            return this.items[this.count-1];
        }
        removeFront(){
            if(this.isEmpty()) return 'queue is null';
            let result = this.items[this.lowestCount];
            delete this.items[this.lowestCount];
            this.lowestCount++;
            return result;
        }
        removeBack(){
            if(this.isEmpty())return "deque is null";
            this.count--;
            let result = this.items[this.count];
            delete this.items[this.count];
            return result;
        }
        toString(){
            if(this.isEmpty())return 'deque is null';
            let objString = `${this.items[this.lowestCount]}`;
            for(let i = this.lowestCount+1;i < this.count; i++){
                objString = `${objString},${this.items[i]}`;
            }
            return objString;
        }
        clear(){
            this.count = 0;
            this.lowestCount = 0;
            this.items = {};
        }
    }

    14.结果

  • 相关阅读:
    activity学习参考
    Activity工作流入门
    Mysql 命令查看函数,触发器。。
    本地redis下载并启动
    Could not initialize class com.taobao.diamond.client.impl.DiamondEnvRepo
    mysql把表(表已有数据)的某列属性由空到非空,以及常用列操作
    easyexcel导出两种方式response返回文件流下载和保存到服务器返回下载链接
    EasyExcel导出带下拉框,并解决导出之后打开总是显示发现不可读取内容
    el-table的fixed固定列属性导致数据错位
    el-input按回车时,解决同时触发回车和失焦事件的问题
  • 原文地址:https://www.cnblogs.com/MySweetheart/p/13211685.html
Copyright © 2020-2023  润新知