码迷,mamicode.com
首页 > 编程语言 > 详细

js数据结构与算法——队列

时间:2019-02-21 21:50:18      阅读:206      评论:0      收藏:0      [点我收藏+]

标签:this   false   OLE   span   lse   priority   shift   ++   .sh   

<script>
        //创建一个队列
        function Queue(){
            let items = [];
            //向队尾添加一个新的项
            this.enqueue = function(element){
                items.push(element)
            }

            //移除队列第一个项,并返回被移除的元素 dequeue
            this.dequeue = function(){
                return items.shift();
            }

            this.front = function(){
                return items[0];
            }

            this.isEmpty = function(){
                return items.length === 0;
            }

            this.size = function(){
                return items.length;
            }
        }

        //使用
        // var queue = new Queue();

        //循环队列
        //游戏击鼓传花
        function flowersGama(nameList,number){
            var queue = new Queue();
            for(let i=0;i<nameList.length;i++){
                queue.enqueue(nameList[i]);
            }
            var failers = ‘‘;
            while(queue.size()>1){
                for(let i=0;i<number;i++){
                    queue.enqueue(queue.dequeue());
                }
                failers = queue.dequeue();
                console.log(failers + ‘被淘汰了‘);
            }
            return queue.dequeue();
        }
        var names = [‘John‘,‘Jack‘,‘Camila‘,‘Ingrid‘,‘Carl‘];
        var winner = flowersGama(names, 7);
        console.log(‘胜利者:‘ + winner); 

        //优先队列

        function PriorityQueue(){
            let 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(let 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);
                    }
                }
            }
            
            this.isEmpty = function(){
                return items.length === 0;
            }

            this.print = function(){
                console.log(items)
            }
        }
        var priorityQueue = new PriorityQueue();
        priorityQueue.enqueue("John", 2);
        priorityQueue.enqueue("Jack", 1);
        priorityQueue.enqueue("Camila", 1);
        priorityQueue.print(); 
    </script>

 

js数据结构与算法——队列

标签:this   false   OLE   span   lse   priority   shift   ++   .sh   

原文地址:https://www.cnblogs.com/huangmin1992/p/10415230.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!