前言
一个简单的队列示例
内容
class Queue {
constructor() {
this.items = {}
this.headIndex = 0
this.tailIndex = 0
}
enqueue(item) {
this.items[this.tailIndex] = item
this.tailIndex++
}
dequeue() {
if (!this.isEmpty) return
const item = this.items[this.headIndex]
delete this.items[this.headIndex]
this.headIndex++
return item
}
get length() {
return this.tailIndex - this.headIndex
}
get isEmpty() {
return this.tailIndex
}
}