概念
队列是一个线性结构,特点是先进先出。
入队push()
:
出队shift()
:
实现
class Queue {
constructor() {
this.queue = []
}
push(item) {
// 入队
this.queue.push(item)
}
shift() {
// 出队
return this.queue.shift()
}
getHeader() {
// 获得队头元素
return this.queue[0]
}
getLength() {
// 获得队列长度
return this.queue.length
}
isEmpty() {
// 判断队列是否为空
return this.getLength() === 0
}
}