class Queue{
constructor() {
this.count = 0
this.list = {}
this.lowestCount=0
}
//往队列添加元素
enqueue (element) {
this.list[this.count] = element
this.count++
}
//检测队列是否为空
isEmpty () {
return this.count-this.lowestCount===0
}
//从队列里移除元素
dequeue () {
if (this.isEmpty()) {
return undefined
}
const result = this.list[this.lowestCount]
delete this.list[this.lowestCount]
this.lowestCount++
return result
}
//查看队列头元素
peek () {
if (this.isEmpty()) {
return undefined
}
return this.list[this.lowestCount]
}
//获取队列长度
size () {
return this.count - this.lowestCount
}
//清空队列
clear () {
this.list = {}
this.count = 0
this.lowestCount=0
}
//字符串方法
toString () {
if (this.isEmpty()) {
return ''
}
let str = `${this.list[this.lowestCount]}`
for (let i = this.lowestCount + 1; i < this.count; i++){
str+=`,${this.list[i]}`
}
return str
}
//
}
JavaScript数据结构之队列
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。
推荐阅读更多精彩内容
- 前面我们学习了栈的实现,队列和栈非常类似,但是使用了不同的原则,而非后进先出。 队列是遵循FIFO(First I...
- 栈(Stack) 栈:又称为栈或堆叠,是计算机科学中的一种抽象数据类型,只允许在有序的线性数据集合的一端(称为堆栈...