


export default class MyCricularQueue {
constructor (k) {
// 保存資料長度為k的長度
this.list = Array(k)
this.front = 0
this.rear = 0
this.max = k
}
enQueue (v) {
if (this.isFull()) {
return false
} else {
this.list[this.rear] = v
this.rear = (this.rear + 1) % this
return true
}
}
deQueue () {
let v = this.list[this.front]
this.list[this.front] = ''
this.front = (this.front + 1) % this.max
return v
}
isEmpty () {
return this.front === this.rear && !this.list[this.front]
}
isFull () {
return this.front === this.rear && this.list[this.front]
}
Front () {
return this.list[this.front]
}
Rear () {
let rear = this.rear - 1
return this.list[rear < 0 ? this.max - 1 : rear]
}
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/24726.html
標籤:其他
上一篇:任務調度器
下一篇:從瀏覽器渲染程序看重繪回流
