『学习JavaScript数据结构与算法』队列篇

队列是遵循先进先出的数据结构。在JavaScript中,利用数组来存储队列中的元素。并提供一些操作队列的方法。队列Queue类代码如下:

 1 function Queue(){
 2     var items = [];//存储队列元素
 3     this.enqueue = function(element){    //进队列
 4         items.push(element);
 5     };
 6     this.dequeue = function(){        //出队列,返回出来的元素
 7         return items.shift();
 8     };
 9     this.front = function(){        //返回队列的头元素
10         return items[0];
11     };
12     this.isEmpty = function(){        //判断队列是否为空
13         return items.length == 0;
14     };
15     this.clear = function(){        //清空队列
16         items = [];
17     };
18     this.size = function(){            //返回队列的长度
19         return items.length;
20     };
21     this.print = function(){        //打印队列
22         console.log(items.toString());
23     };
24 }
Queue

 

此外,在此基础上还有优先队列。即元素的添加和移除是基于优先级的。在进队列时,按照元素的优先级大小把它放在适当的队列位置中。优先队列PriorityQueue类代码如下:

 1 //优先队列
 2 function PriorityQueue(){
 3     var items = [];//存储队列元素
 4     function QueueElement (element,priority){    //队列元素 由元素值和元素的优先级组成
 5         this.element = element;
 6         this.priority = priority;
 7     }
 8 
 9     this.enqueue = function(element,priority){    //进队列
10         var queueElement = new QueueElement(element,priority);
11         if(this.isEmpty()){        //如果队列没有元素,则直接插入到队列末尾
12             items.push(queueElement);
13         }else{
14             var added = false;
15             for(var i=0;i<items.length;i++){
16                 if(queueElement.priority < items[i].priority){
17                     items.splice(i,0,queueElement);
18                     added = true;
19                     break;
20                 }
21             }
22             //如果优先级比现有队列中的元素的优先级都要大,则插到队列末尾
23             if(!added){
24                 items.push(queueElement);
25             }
26         }
27     };
28     this.dequeue = function(){        //出队列,返回出来的元素
29         return items.shift();
30     };
31     this.front = function(){        //返回队列的头元素
32         return items[0];
33     };
34     this.isEmpty = function(){        //判断队列是否为空
35         return items.length == 0;
36     };
37     this.clear = function(){        //清空队列
38         items = [];
39     };
40     this.size = function(){            //返回队列的长度
41         return items.length;
42     };
43     this.print = function(){        //打印队列
44         var q='';
45         for(var i=0;i<items.length;i++){
46             q+=items[i].element+' ';
47         }
48         console.log(q);
49     };
50 }
PriorityQueue

创建一个PriorityQueue实例看一下效果:

var priorityQueue = new PriorityQueue();
priorityQueue.enqueue("John",2);
priorityQueue.enqueue("Jack",1);
priorityQueue.enqueue("Camila",1);
priorityQueue.print();    //Jack Camila John

 

另一个修改版的队列:循环队列。模拟一个击鼓传花游戏来实现循环队列:

 1 //击鼓传花
 2 function hotPotato (nameList,num){
 3     var queue = new Queue();
 4 
 5     for(var i=0;i<nameList.length;i++){
 6         queue.enqueue(nameList[i]);
 7     }
 8 
 9     var eliminated = '';
10     while (queue.size()>1){
11         for(var i=0;i<num;i++){
12             queue.enqueue(queue.dequeue());        //循环操作
13         }
14         eliminated = queue.dequeue();
15         console.log(eliminated + '在击鼓传花游戏中被淘汰。');
16     }
17 
18     return queue.dequeue();
19 }
20 var names = ['John','Jack','Camila','Ingrid','Carl'];
21 var winner = hotPotato(names,7);
22 console.log('胜利者:'+ winner);//胜利者:John
循环队列

 

posted @ 2016-02-29 12:08  Evercx  阅读(148)  评论(0编辑  收藏  举报