zoukankan      html  css  js  c++  java
  • 线性结构_优先级队列

    优先级队列

                1. 特点:

                    1. 普通队列新插入的元素,默认会被放在队列的尾部

                    2. 优先级队列中的每一个元素都有一个优先级属性

                    2. 优先级队列会将新插入的元素的优先级与队列中的元素进行比较,然后根据优先级,将新元素放置在正确的位置

                    3. 优先级队列的其他属性和方法与普通队列无异


                2. 应用场景

                    登机时,按照商务舱,经济舱顺序等级

                    病人排队时,根据病情轻重排序


                3. 优先级队列的方法

                    1. enqueue(element, priority)       向队列添加一个元素

                    2. dequeue()                        从队列头部移除一个元素并返回

                    3. front()                          返回队列中的第一个元素,此时队列没有被修改

                    4. isEmpty()                        判断队列是否为空

                    5. size()                           反对队列长度,即队列中元素的个数

                    6. toString()                       将队列中的元素转成字符串并返回

       4. 双向队列的代码实现

       function PriorityQueue(){
           // 在改优先级队列中,优先级数值越小,表示优先级越高
        
            // 封装一个内部类
            function QueueElement(item, priority){
                this.item = item;
                this.priority = priority;
            }
    
            // 优先级队列的属性
            this.items = [];
    
            // 优先级队列的方法
            // 1. 向队列添加一个元素
            PriorityQueue.prototype.enqueue = function(item, priority = 0){
                // 新建优先级队列的元素对象
                var elem = new QueueElement(item, priority);
                if(this.items.length == 0){
                    this.items.push(elem);
                }else{
                    for(var i = 0; i < this.items.length; i++){
                        // 依次比较优先级数值大小
                        if(elem.priority < this.items[i].priority){
                            this.items.splice(i, 0, elem);
                            break;
                        }
                    }
    
                    // 若所有的优先级数值都比自己小,则自己添加在最后,注意此时的 i 已经比队列的长度大 1。
                    if(i == this.items.length){
                        this.items.push(elem);
                    }
    
                }
            }
    
            // 2. 从队列头部移除一个元素并返回
            PriorityQueue.prototype.dequeue = function(){
                return this.items.shift();
            }
    
            // 3. 返回队列中的第一个元素,此时队列没有被修改
            PriorityQueue.prototype.front = function(){
                return this.items[0];
            }
    
            // 4. 判断队列是否为空
            PriorityQueue.prototype.isEmpty = function(){
                return this.items.length == 0;
            }
    
            // 5. 反对队列长度,即队列中元素的个数
            PriorityQueue.prototype.size = function(){
                return this.items.length;
            }
    
            // 6. 将队列中的元素转成字符串并返回
            PriorityQueue.prototype.toString = function(){
                var res = "";
                for(var i = 0; i < this.items.length; i++){
                    res += this.items[i].item + "-" + this.items[i].priority + " ";
                }
                return res;
            }
       }
    View Code
  • 相关阅读:
    设计模式之迭代器与组合模式(三)
    设计模式之迭代器与组合模式(二)
    设计模式之迭代器与组合模式(一)
    设计模式之模板方法模式(一)
    设计模式之模板方法模式(三)
    设计模式之模板方法模式(二)
    Spring Cloud微服务初探
    设计模式之适配器模式与外观模式(二)
    设计模式之适配器模式与外观模式(一)
    设计模式之命令模式(三)
  • 原文地址:https://www.cnblogs.com/carreyBlog/p/13657023.html
Copyright © 2011-2022 走看看