STM32 ADC
阿新 • • 發佈:2022-05-11
雙端佇列
/** * 雙端佇列 */ class Deque { constructor() { // 隊列當前索引 this.count = 0 // 隊頭索引 this.lowestCount = 0 // 儲存佇列 this.items = {} } /** * 新增到隊頭 */ addFront(element) { if (this.isEmpty()) { this.addBack(element) } else if (this.lowestCount > 0) { this.lowestCount-- this.items[this.lowestCount] = element } else { // 將所有元素向後移動一位 for (let i = this.count; i > 0; i--) { this.items[i] = this.items[i - 1] } this.count++ this.lowestCount = 0 this.items[0] = element } } /** * 新增到隊尾 */ addBack(element) { this.items[this.count] = element this.count++ } /** * 移除隊頭 */ removeFront() { if (this.isEmpty()) { return undefined } else { const res = this.items[this.lowestCount] delete this.items[this.lowestCount] this.lowestCount++ return res } } /** * 移除隊尾 */ removeBack() { if (this.isEmpty()) { return undefined } else { this.count-- const res = this.items[this.count] delete this.items[this.count] return res } } /** * 返回隊頭 */ peekFront() { if (this.isEmpty()) { return undefined } else { return this.items[this.lowestCount] } } /** * 新增到隊尾 */ peekBack() { if (this.isEmpty()) { return undefined } else { return this.items[this.count - 1] } } /** * 檢視佇列是否為空 */ isEmpty() { return this.count - this.lowestCount === 0 } /** * 檢視佇列長度 */ size() { return this.count - this.lowestCount } /** * 清空佇列 */ clear() { this.count = 0 this.lowestCount = 0 this.items = {} } }