JavaScript数据结构之优先队列和循环队列

JavaScript数据结构之优先队列和循环队列

优先队列

所谓优先队列就是基于优先级实现队列的添加和删除

最小优先队列(优先添加,正常出列)代码如下:

 function Queue() {
        //初始化队列(使用数组实现)
        var items = [];
        //向队列(尾部)中插入元素
        this.enqueue = function(element) {
            items.push(element);
        }
        //从队列(头部)中弹出一个元素,并返回该元素
        this.dequeue = function() {
            return items.shift();
        }
        //查看队列最前面的元素(数组中索引为0的元素)
        this.front = function() {
            return items[0];
        }
        //查看队列是否为空,如果为空,返回true;否则返回false
        this.isEmpty = function() {
            return items.length == 0;
        }
        //查看队列的长度
        this.size = function() {
            return items.length;
        }
        //查看队列
        this.print = function() {
            //以字符串形势返回
            return items.toString();
        }
    }
	 function PriorityQueue() {
	        var items = [];
	        //需要插入队列的元素(该元素为对象,包括值和优先级)
	        function QueueElement(element, priority) {
	            this.element = element;
	            this.priority = priority;
	        }
	
	        //插入元素到队列中的方法
	        this.enqueue = function (element, priority) {
	            //需要插入队列的元素
	            var queueElement = new QueueElement(element, priority);
	
	            if(this.isEmpty()) {
	                //当队列为空时,直接往队列中添加元素
	                items.push(queueElement);
	            }else{
	                //当队列不为空时,遍历队列中的元素,当需要添加的元素的优先级小于(队列中)当前元素的优先级,就把该元素插入到当前元素之前
	                var added = false;
	                for(var i = 0; i < items.length; i++){
	                    if(queueElement.priority < items[i].priority) {
	                        items.splice(i, 0, queueElement);
	                        added = true;
	                        break;//终止队列循环
	                    }
	                }
	                //当需要添加的元素的优先级比队列中任何一个元素的优先级都要高时,把该元素插入到队列的末尾
	                if(!added){
	                    items.push(queueElement);
	                }
	            }
	        }
	
	        //查看队列是否为空,如果为空,返回true;否则返回false
	        this.isEmpty = function() {
	            return items.length == 0;
	        }    
	
	        //查看队列
	        this.print = function() {
	            return items;
	        }        
	    }
	    var priorityQueue = new PriorityQueue();
	    priorityQueue.enqueue('a', 10);
	    priorityQueue.enqueue('b', 3);
	    priorityQueue.enqueue('c', 8);
	    priorityQueue.enqueue('d', 8);
	    priorityQueue.enqueue('e', 20);
	    console.log(priorityQueue.print());

在这里插入图片描述

循环队列

所谓的循环队列就是每次循环的时候(从队列头部)弹出一个数值,再把这个数值加入到队列的尾部,循环 n 次(自定义n值),循环停止时弹出队列头部的值,直到队列中只剩下一个值。
代码如下:

function Queue() {
        
        //初始化队列(使用数组实现)
        var items = [];

        //向队列(尾部)中插入元素
        this.enqueue = function(element) {
            items.push(element);
        }

        //从队列(头部)中弹出一个元素,并返回该元素
        this.dequeue = function() {
            return items.shift();
        }

        //查看队列最前面的元素(数组中索引为0的元素)
        this.front = function() {
            return items[0];
        }

        //查看队列是否为空,如果为空,返回true;否则返回false
        this.isEmpty = function() {
            return items.length == 0;
        }

        //查看队列的长度
        this.size = function() {
            return items.length;
        }

        //查看队列
        this.print = function() {
            //以字符串形势返回
            return items.toString();
        }
    }
    //nameList 数组
    //num 指定的传递次数
    function hotPotato(nameList, num) {

        var queue = new Queue();

        //把名单插入队列
        for(var i = 0; i < nameList.length; i++) {
            queue.enqueue(nameList[i]);
        }

        //初始值
        var eliminated = '';

        //当队列里的个数大于1时,继续传递
        while(queue.size() > 1) {
            for(var i = 0; i < num; i++) {
                //每次把队列头部弹出的值再次插入队列的尾部,行程一个循环队列
                queue.enqueue(queue.dequeue());
            }
            //当循环停止时,即到了指定的次数时,弹出队列头部的值
            eliminated = queue.dequeue();
            console.log(eliminated + '弹出');
        }

        //只剩最后一个值时结束
        return queue.dequeue();
    }

    var names = ['a', 'b', 'c', 'd', 'e'];
    var last = hotPotato(names, 5);
    console.log("最后留下的值:"+last);

在这里插入图片描述

发布了102 篇原创文章 · 获赞 252 · 访问量 2万+

猜你喜欢

转载自blog.csdn.net/low666/article/details/104921820