Appearance
队列
查看js的队列结构
js
class Queue {
constructor(...args) {
this.queue = [...args]
}
// 入队列
enqueue(...items) {
this.queue.push(...items)
}
// 出队列
dequeue() {
return this.queue.shift()
}
// 队头
front() {
return this.isEmpty() ? undefined : this.queue[0]
}
// 队尾
back() {
return this.isEmpty() ? undefined : this.queue[this.size() - 1]
}
// 是否为空
isEmpty() {
return this.size() === 0
}
// 队列长度
size() {
return this.queue.length
}
}