zoukankan      html  css  js  c++  java
  • JS数据结构及算法(二) 队列

    队列是遵循先进先出的一种数据结构,在尾部添加新元素,并从顶部移除元素。

    1.普通队列

    function Queue() {
        this.items = [];
    }
    
    Queue.prototype = {
        enqueue: function (element) {
            this.items.push(element);
        },
        dequeue: function () {
            return this.items.shift();
        },
        front: function () {
            return items[0];
        },
        isEmpty: function () {
            return this.items.length === 0;
        },
        clear: function () {
            this.items = [];
        },
        size: function () {
            return this.items.length;
        },
        print: function () {
            console.log(this.items.toString());
        }
    };
    

    2.优先队列:元素的添加基于优先级

    //继承Queue
    function PriorityQueue() {
        Queue.call(this);
    }
    PriorityQueue.prototype = Object.create(Queue.prototype);
    PriorityQueue.prototype.constructor = PriorityQueue;
    PriorityQueue.prototype.enqueue = function (element,priority) {
        function QueueElement (element, priority){
            this.element = element;
            this.priority = priority;
        }
        let queueElement = new QueueElement(element, priority);
    
        let added = false;
        for (let i=0; i<this.items.length; i++){
            if (queueElement.priority < this.items[i].priority){ 
                this.items.splice(i,0,queueElement);             
                added = true;
                break; 
            }
        }
        if (!added){
            this.items.push(queueElement);
        }
    };
    PriorityQueue.prototype.print=function () {
        for(let i=0;i<this.items.length;i++){
            console.log(`${this.items[i].element}  - ${this.items[i].priority}`);
        }
    };
    

     3.循环队列(击鼓传花模拟)

    function hotPotato (nameList, num){
    
        let queue = new Queue();
    
        for (let i=0; i<nameList.length; i++){
            queue.enqueue(nameList[i]); //所有名单加入队列
        }
    
        let eliminated = '';
        while (queue.size() > 1){
            for (let i=0; i<num; i++){
                queue.enqueue(queue.dequeue()); //从队列开头移除一项,再将其添加到队列末尾
            }
            eliminated = queue.dequeue();   //一旦传递次数到达指定的数字,拿着花的人就被淘汰了(从队列中移除)
            console.log(eliminated + ' was eliminated from the Hot Potato game.');
        }
    
        return queue.dequeue(); //最后剩下的人是胜者
    }
    
    let names = ['John','Jack','Camila','Ingrid','Carl'];
    let winner = hotPotato(names, 7);
    console.log('The winner is: ' + winner);
    

    参考:《学习JavaScript数据结构与算法(第二版)》

  • 相关阅读:
    [BZOJ1013][JSOI2008]球形空间产生器sphere 高斯消元
    [Luogu1848][USACO12OPEN]书架Bookshelf DP+set+决策单调性
    [BZOJ1025][SCOI2009]游戏 DP+置换群
    [BZOJ1024][SCOI2009]生日快乐 搜索
    [BZOJ2002][Hnoi2010]Bounce弹飞绵羊 LCT
    「BZOJ 4565」「HAOI 2016」字符合并「区间状压DP」
    「BZOJ 5161」最长上升子序列「状压DP」
    「SPOJ TTM 」To the moon「标记永久化」
    「学习笔记」字符串大礼包
    「CF724G」Xor-matic Number of the Graph「线性基」
  • 原文地址:https://www.cnblogs.com/jingmi-coding/p/9284824.html
Copyright © 2011-2022 走看看