• js 队列


    <html lang="en">
    <head>
        <meta charset="UTF-8">
        <meta name="viewport" content="width=device-width, initial-scale=1.0">
        <meta http-equiv="X-UA-Compatible" content="ie=edge">
        <title>Document</title>
    </head>
    <body>
        <script>
            function Queue(){
                let item = [];
                this.enqueue=function(element){
                    item.enqueue(element);
                }
                // rem 第一项
                this.dequeue=function(){
                    item.shift();
                }
                // 返回第一个
                this.front=function(){
                    return item[0];
                }
                // 检查栈是否为空
                this.isEmpty= function(){
                    return (item.length == 0);
                }
                // 长度
                this.size= function(){
                    return item.length;
                }
                // 打印队列
                this.print=function(){
                    console.log( item.toString() );
                }
            }
    
            function PriorityQueue(){
                item = [];
                // element 元素 priorty 优先级
                function QueueElement ( element , priority ){
                    this.element = element;
                    this.priority = priority;
                
                }
    
                // 检查栈是否为空
                this.isEmpty= function(){
                    return (item.length == 0);
                }
                this.enqueue = function( element,priority ){
                    let queueElement = new QueueElement( element , priority );
    
                    var added = false;
                    if ( item.length !=0 ){
                        for( let i=0; i<item.length; i++){
                            
                            // 通过遍历 当前的 优先级也就是 ( item中 )priority 是否大于 我们要插入的值的 优先级
                            if( queueElement.priority < item[i].priority ){  // 
                                added = true;
                                item.splice( i , 0 , queueElement );  // splice 在指定 i 位置前 插入我们的元素
                                break ;
                            }
                        }
                            if( added == false ){
                                // 在 否的情况下 直接 在数组末尾进行插入就 OK
                                item.push( queueElement );
                            }
                    }else{
                        item.push(queueElement)
                    }
                
    
                    // 输出-> 打印
                    this.print = function(){
                        for( let i=0; i<item.length; i++){
                            console.log( `${item[i].element} - ${item[i].priority}` )
                        }
                        console.log(item.length)
                    }
                }
            }
            
            let priorityQueue = new PriorityQueue();
            priorityQueue.enqueue('johh',2);
            priorityQueue.enqueue('tom',3);
            priorityQueue.enqueue('motn',1);
            priorityQueue.enqueue('motn',2);
            priorityQueue.print()
        </script>
    </body>
    </html>
  • 相关阅读:
    C++中虚函数
    ES6入门四:对象字面量扩展与字符串模板字面量
    ES6入门三:解构
    ES6入门二:默认值与默认值表达式
    ES6入门一:块级作用域(let&const)、spread展开、rest收集
    JavaScript严格模式
    JavaScript中with不推荐使用,为什么总是出现在面试题中?
    ES6入门一:ES6简介及Babel转码器
    HTML5之websocket
    HTML5之fileReader异步读取文件及文件切片读取
  • 原文地址:https://www.cnblogs.com/ar13/p/8000719.html
Copyright © 2020-2023  润新知