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) 棧:又稱為棧或堆疊,是計算機科學中的一種抽象數據類型,只允許在有序的線性數據集合的一端(稱為堆棧...