队列
队列结构
队列的创建 -- 数组方法
代码实现
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>封装队列</title>
</head>
<body>
<script>
function Queue(){
// 属性
this.items = [];
// 方法
// 1.将元素加入到队列
Queue.prototype.enqueue = function(element){
this.items.push(element);
};
// 2.从队列中删除前端元素
Queue.prototype.delqueue = function(element){
// 记住这里得有返回值不然后面击鼓传花是undefined,因为函数默认返回值是undefined
return this.items.shift(element);
};
// 3.查看前端元素
Queue.prototype.front = function(){
return this.items[0];
}
// 4.查看队列是否为空
Queue.prototype.isEmpty = function(){
return this.items.length == 0;
}
// 5.查看队列中元素个数
Queue.prototype.size = function(){
return this.items.length;
}
// 6.toString方法
Queue.prototype.toString = function(){
return this.items.join("");
}
}
let queue = new Queue();
console.log(queue.isEmpty());
console.log(queue.size());
queue.enqueue(1);
queue.enqueue(10);
queue.enqueue(8);
queue.enqueue(7);
console.log(queue);
console.log(queue.front());
console.log(queue.isEmpty());
console.log(queue.size());
console.log(queue.toString());
queue.delqueue();
console.log(queue);
console.log(queue.front());
console.log(queue.isEmpty());
console.log(queue.size());
console.log(queue.toString());
</script>
</body>
</html>
队列的实际运用 -- 击鼓传花
题目:
这里其实并不好思考到和队列搭上边,建议看代码前,读者先仔细想想,自己做的时候会怎么做!
想和队列搭上边,就得思考到,围成一个圈,然后每次数到特定的就移除,然后接着数!
代码
// 面试题:击鼓传花
function passGame(nameList,num){
// 1 创建一个队列
let queue1 = new Queue();
// 2 将值都传给队列
for(i of nameList){
console.log(i);
queue1.enqueue(i);
}
// 得一直循环到只剩一个人
while(queue1.size() > 1){
// 数字之前的人重新加入到队列
//这里数组做多了的读者可能会和我一样,一开始感觉会不会越界,其实这个根本没用到下标访问,只是一直循环,操作交给了queue
for(let i = 0;i<num-1;i++){
queue1.enqueue(queue1.delqueue())
}
// num对应的人,直接删除
queue1.delqueue();
}
console.log(queue1.front());
return nameList.indexOf(queue1.front());
}
let arr = ["1","2","3","4","5"];
console.log(passGame(arr,3));
优先级队列
例子
最常见的例子就是操作系统中的线程,重要的先执行不重要的后执行(当然电脑肯定不是按照接下来的代码实现的,只是操作过程类似)
优先级队列实现 -- 数组方法
和队列一样,只是添加元素需要考虑插入地点,以及转变为字符串需要改变一下输出方式,不然会变成很多object !
代码:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
<script>
function PriorityQueue(){
// 封装类 --> 在PriorityQueue里面重新封创建了一个类:可以理解为内部类
function QueueElement(element,priority){
this.element = element;
this.priority = priority;
}
// 属性
this.items = [];
// 实现插入方法
PriorityQueue.prototype.enqueue = function(element,priority){
let queueElement = new QueueElement(element,priority);
if(this.items.length === 0){
this.items.push(queueElement);
}else{
let added = false;
for(let i = 0;i<this.items.length;i++){
if(queueElement.priority < this.items[i].priority){
this.items.splice(i,0,queueElement);
added = true;
break;
}
}
if(added == false){
this.items.push(queueElement);
}
}
}
// 2.从队列中删除前端元素
PriorityQueue.prototype.delqueue = function(element){
return this.items.shift(element);
};
// 3.查看前端元素
PriorityQueue.prototype.front = function(){
return this.items[0];
}
// 4.查看队列是否为空
PriorityQueue.prototype.isEmpty = function(){
return this.items.length == 0;
}
// 5.查看队列中元素个数
PriorityQueue.prototype.size = function(){
return this.items.length;
}
// 6.toString方法
PriorityQueue.prototype.toString = function(){
let a = [];
for(let i =0;i < this.items.length;i++){
a[i] = this.items[i].element + "-" + this.items[i].priority
}
return a.join(" ");
}
}
var pq = new PriorityQueue();
pq.enqueue('abc',111);
pq.enqueue('abd',11);
pq.enqueue('nbc',50);
pq.enqueue('dfg',90);
pq.enqueue('bvn',3);
console.log(pq);
pq.delqueue();
console.log(pq.toString());
</script>
</body>
</html>
总结
这里总结一下,现在写的不管是栈还是队列,都是通过数组实现的,而数组又提供了很多方便的操作,所以基本上都是数组方法的调用,所以理解起来很简单,写起来也很简单!但是这里要理解的也并不是数组的操作,而是如何定义数据结构,使其能实现堆或者列表!
下一节,链表才算真正进入了数据结构的正轨!