1. 基于数组实现队列
将数组及数组的方法封装成一个队列
//基于数组实现队列的封装
function Queue(){
//队列属性
this.item=[];
//队列的基本操作
//向队列尾部添加一个或多个新的项
Queue.prototype.enQueue=function (Element){
this.item.push(Element);
}
//从队列首部删除元素,并返回被删除的元素
Queue.prototype.deQueue=function(){
return this.item.shift();
}
//返回队列首部第一个元素
Queue.prototype.Front=function(){
return this.item[0];
}
//队列是否为空,是返回true,否返回false
Queue.prototype.IsEmpty=function(){
return (this.item.length)?false:true;
}
//返回队列的元素个数
Queue.prototype.Size=function(){
return this.item.length;
}
//将队列转换成字符串形式
Queue.prototype.toString=function(){
return this.item.join(" ");
}
}
//使用队列
var queue=new Queue();
queue.enQueue(1);
queue.enQueue(2);
queue.enQueue(3);
console.log(queue);//[1,2,3]
console.log("队列元素个数:"+queue.Size());//3
console.log("队列为空吗?"+queue.IsEmpty());//false
console.log("队列首部的元素为:"+queue.Front());//1
console.log('删除队列首部元素:'+queue.deQueue());//1
console.log('以字符串形式打印队列:'+queue.toString());//2 3
结果:
练习:击鼓传花游戏
规则:几个朋友一起玩一个游戏,围成一圈,开始数数,数到某个数字的人自动淘汰,最后剩下的人获得胜利,请问最后剩下的是原来在哪一个位置的人?
参数:所有人的姓名及位置
结果:最终剩下的一个人的位置及姓名
思路:用队列来做,让所有人数数,数到规定的数的那个人从队列中删除,其余人从队首放到队尾,根据是队列从首部删元素,尾部放元素,直到队列元素为1时停止游戏。
//基于数组实现队列的封装
function Queue(){
//队列属性
this.item=[];
//队列的基本操作
//向队列尾部添加一个或多个新的项
Queue.prototype.enQueue=function (Element){
this.item.push(Element);
}
//从队列首部删除元素,并返回被删除的元素
Queue.prototype.deQueue=function(){
return this.item.shift();
}
//返回队列首部第一个元素
Queue.prototype.Front=function(){
return this.item[0];
}
//队列是否为空,是返回true,否返回false
Queue.prototype.IsEmpty=function(){
return (this.item.length)?false:true;
}
//返回队列的元素个数
Queue.prototype.Size=function(){
return this.item.length;
}
//将队列转换成字符串形式
Queue.prototype.toString=function(){
return this.item.join("\n");
}
}
//使用队列
var queue=new Queue();
//击鼓传花游戏
//定义参加游戏的人及其位置
let map=([[0,'张三'],[1,'王五'],[2,'李四'],[3,'赵六'],[4,'王大花'],[5,'牛爱花']]);
var array=Array.from(map);
console.log('游戏开始前,参加人名单如下:');
for(var [k,v] of array.entries()){
queue.enQueue(v);
console.log(v);
}
//定义要传的数字
var data=3;
//进行游戏
while(queue.Size()>1){
while(data>=1){
if(data!=1){
queue.enQueue(queue.deQueue());
}
data--;
}
queue.deQueue();
data=3;
}
//判定最后胜出者,打印名单
console.log('最后剩下人的人名单:'+queue.Front());
结果:
2.基于数组实现优先级队列
- 优先级队列介绍
对于优先级队列,在插入一个元素的时候会考虑该数据的优先级。和其他数据优先级进行比较,比较完成后,可以得出这个元素在队列中的位置,其他处理方式和基本队列的处理方式一样。 - 优先级队列主要考虑的问题
每个元素不在是一个数据,而且包含数据的优先级,在添加方式中,根据优先级放入正确的位置。 - 代码实现
//优先级队列实现
function PriorityQueue(){
//封装属性
this.item=[];
//在PriorityQueue创建一个内部类
function QueueElement(element,priority){
this.element=element;
this.priority=priority;
}
//实现插入方法
PriorityQueue.prototype.enQueue=function(element,priority){
//创建QueueElement对象
var queueElement=new QueueElement(element,priority);
//判断队列是否空,空队列直接插入元素
if(this.item.length==0){
this.item.push(queueElement);
}
//不为空的话,遍历整个队列,如果要插入的元素优先级比队列中某个元素的优先级小,就把要插入的元素放在这个元素的前面
else {
var flag=false;
for(var i=0;i<this.item.length;i++){
if(queueElement.priority<this.item[i].priority){
this.item.splice(i,0,queueElement);
flag=true;
break;
}
}
}
//如果遍历完整个队列,发现要插入的元素优先级比队列中所有元素优先级大,则直接插入队列尾部
if(flag==false){
this.item.push(queueElement);
}
}
//从队列首部删除元素,并返回被删除的元素
PriorityQueue.prototype.deQueue=function(){
var data=this.item.shift();
return data.element+" "+data.priority;
}
//返回队列首部第一个元素
PriorityQueue.prototype.Front=function(){
return this.item[0].element+" "+this.item[0].priority;
}
//队列是否为空,是返回true,否返回false
PriorityQueue.prototype.IsEmpty=function(){
return (this.item.length)?false:true;
}
//返回队列的元素个数
PriorityQueue.prototype.Size=function(){
return this.item.length;
}
//将队列转换成字符串形式
PriorityQueue.prototype.toString=function(){
var str='';
for(var i=0;i<this.item.length;i++){
str+=this.item[i].element+' '+this.item[i].priority+'\n';
}
return str;
}
}
//使用优先级队列
var priorityQueue=new PriorityQueue();
priorityQueue.enQueue('abc',30);
priorityQueue.enQueue('jc',10);
priorityQueue.enQueue('hew',1);
priorityQueue.enQueue('a',300);
console.log(priorityQueue);
console.log("队列元素个数:"+priorityQueue.Size());//4
console.log("队列为空吗?"+priorityQueue.IsEmpty());//false
console.log("队列首部的元素为:"+priorityQueue.Front());//hew 1
console.log('删除队列首部元素:'+priorityQueue.deQueue());//hew 1
console.log('以字符串形式打印队列:\n'+priorityQueue.toString());//jc 10
// abc 30
// a 300
结果: