class Queue{
constructor(){
this.count = 0;
this.lowestCount = 0;
this.items = {}
}
enqueue(ele){ //向队尾添加一个或多个项
this.items[this.count] = ele
this.count++
}
dequeue(){ //移除队列第一项
if(this.isEmpty()){
return undefined
}
const result = this.items[this.lowestCount]
delete this.items[this.lowestCount]
this.lowestCount++
return result
}
peek(){ //返回队列中第一个元素
if(this.isEmpty()){
return undefined
}
return this.items[this.lowestCount]
}
isEmpty(){
return this.count - this.lowestCount === 0
}
size(){
return this.count - this.lowestCount
}
clear(){
this.count = 0;
this.lowestCount = 0;
this.items = {}
}
toString(){
if(this.isEmpty){
return ""
}
let objString = `${this.items[this.lowestCount]}`
for(let i = this.lowestCount+1; i<this.count; i++){
objString = `${objString}, ${this.items[i]}`
}
return objString
}
}
let myQueue = new Queue()
myQueue.enqueue(12)
myQueue.enqueue(45)
console.log(myQueue.isEmpty())
console.log(myQueue.toString())
console.log(myQueue.dequeue())
console