栈和队列都是一种受限的线性结构
队列是数据从表的后端rear进行插入,表的前端front删除操作。
应用场景:打印队列、线程队列
基于数组实现的Demo
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>封装队列基于数组实现</title>
</head>
<body>
<script>
//封装队列
function Queue(){
//属性
this.items = []
//方法
//1.将元素加入到队列中
Queue.prototype.enqueue = function(element){
this.items.push(element)
}
//2.从队列中删除前端元素
Queue.prototype.dequeue = function(){
return this.items.shift()
}
//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(){
var resultString = ''
for(let i=0;i<this.items.length;i+&#