1.队列的限制是先进先出
2.封装队列
//封装队列类
function Queue() {
//属性
this.items = [];
//方法
//1.入队
Queue.prototype.enQueue = (elem) => {
this.items.push(elem);
}
//2.出队
Queue.prototype.deQueue = () => {
//数组第一个元素删除
return this.items.shift();
}
//3.返回对首元素
Queue.prototype.front = function () {
return this.items[0];
}
//4.队列是否为空
Queue.prototype.isEmpty = () => this.items.length == 0;
//5.队列大小
Queue.prototype.size = () => this.items.length;
//6.toString
Queue.prototype.toString = () => {
var resultStr = '';
for(var i = 0; i < this.items.length; i++){
resultStr += this.items[i] + ' ';
}
return resultStr;
}
}
3.测试
我们可以看到第一次打印队列q的时候,队列明明有4个值,但是只显示三个。console.log是一个动态打印,打印的对象又是一个引用地址,后面发生了改变,造成conlog打的值也改变了。
可以时时将队列转化为字符串,然后再打印,这样就对了。
//测试
var q = new Queue();
q.enQueue('刘备');
q.enQueue('关羽');
q.enQueue('张飞');
q.enQueue('赵云');
console.log(q.front());
console.log(q);
console.log(q.toString());
q.deQueue();
console.log(q.front());
console.log(q);
console.log(q.isEmpty());
console.log(q.size());
console.log(q.toString());
4.击鼓传花,要有一种相对运动的思想,原本是花在传,现在变为传到谁,谁就在队首,若没结束,则出队,入队到队尾。
//击鼓传花
//namelist是一个名字数组,num是花号,比如5,就数到第5个人淘汰
var passGame = function (namelist,num) {
let q = new Queue();
for (let i = 0; i < namelist.length; i++) {
q.enQueue(namelist[i]);
}
// console.log(q); 测试队列插入成功否
//只要剩的人数>1就继续传
while (q.size() > 1) {
//num前几个都退队,再插入队列
for (var i = 0; i < num - 1; i++) {
q.enQueue(q.deQueue());
}
//第num个数,退队,一轮结束
q.deQueue();
}
console.log(q.size());
var endname = q.front();
console.log(endname);
console.log(namelist.indexOf(endname));
}
//测试
var hero = ['刘备','关羽','张飞','赵云','曹操','典韦','孙策','周瑜'];
passGame(hero ,3);
5.优先级队列,插入的时候,会根据优先级到适当的位置
6.优先级队列的实现,保存的元素由元素值和优先级组成,插入的时候要判断优先级,插入适当的位置
插入方法传入的值是(元素值,优先级),传入之后要先创建个元素的对象接收这两个值!!!
//封装优先级队列
function PriorityQueue() {
//封装队列元素:值+优先级
function QueueElement(element,priority) {
this.element = element;
this.priority = priority;
}
//封装属性
this.items = [];
//1.插入方法
PriorityQueue.prototype.enQueue = function (element,priority) {
//首先要创建队列元素的对象!!
var queueElement = new QueueElement(element,priority);
//判断队列为空,直接插入
if(this.items.length == 0){
this.items.push(queueElement);
}else {
var added = false;
for(var i = 0; i< this.items.length; i++){
//优先级数字小的在前面,表示优先级高
if(queueElement.priority < this.items[i].priority){
//在索引i处插入元素
this.items.splice(i,0,queueElement);
added = true;
break;
}
}
//遍历完万一没有比它优先级低的,那就插在最后面
if(!added){
this.items.push(queueElement);
}
}
}
//2.出队
PriorityQueue.prototype.deQueue = () => {
//数组第一个元素删除
return this.items.shift();
}
//3.返回对首元素
PriorityQueue.prototype.front = function () {
return this.items[0];
}
//4.队列是否为空
PriorityQueue.prototype.isEmpty = () => this.items.length == 0;
//5.队列大小
PriorityQueue.prototype.size = () => this.items.length;
//6.toString
PriorityQueue.prototype.toString = () => {
var resultStr = '';
for(var i = 0; i < this.items.length; i++){
resultStr += this.items[i].element +'-' + this.items[i].priority + ' ';
}
return resultStr;
}
}
测试
//测试
var pq = new PriorityQueue();
pq.enQueue('关羽',3);
pq.enQueue('曹操',13);
pq.enQueue('吕布',1);
pq.enQueue('张辽',6);
console.log(pq.size());
console.log(pq.toString());
pq.deQueue();
pq.deQueue();
console.log(pq.size());
console.log(pq.front());
console.log(pq.toString());