队列-什么是队列?
概念:队列也是一种受限制的线性表,是数据结构中一种抽象的数据存储结构,与栈类似,但是它遵循先进先出的原则
生活中有哪些队列呢,让我们思考下......
例子:排队买东西,都是文明人谁也不要插队额,这是不是就是一个很典型的队列结构呢?下面让我们用代码实现下队列吧
class Queue {
constructor(){
this.list=[] //初始化队列数组
}
// 加入队列
enqueue(item){
this.list.push(item)
}
// 删除队列
dequeue(){
return this.list.shift()
}
// 查看队列长度
lequeue(){
return this.list.length
}
// 查看队列队首元素
queueheader(){
return this.list[0]
}
// 查看队列队尾元素
queuefooter(){
return this.list[this.lequeue()-1]
}
// 清空队列
clearqueue(){
this.list=[];
}
}