JavaScript是當下最流行的編程語言之一,它能夠作不少事情:算法
並且目前大部分編程語言的高級應用都會用到數據結構與算法以及設計模式。編程
隊列是遵循FIFO(First In First Out,先進先出,也稱爲先來先服務)原則的一組有序的項。
隊列在尾部添加新元素,並從頂部移除元素。最新添加的元素必須排在隊列的末尾 。小程序
我看有不少文檔都是說隊列就像買什麼東西排隊,我認爲這個比喻用在標準隊列上不恰當。 數據結構
我以爲標準隊列像是一段管道,每次都只能放一個球進去,上邊只用來放球(入隊),因爲地球引力,球會從下邊的口出去(出隊)。編程語言
隊列:這段能夠放球的管道就是隊列
元素:管道里的球
隊首:在當前管道里的球最先放進去的那個球的位置,同時也是第一個掉出去的球
隊尾:在當前管道里的球最後放進去的那個球的位置,同時也是最後一個掉出去的球this
class Queue { constructor() { this.count = 0; // 整個隊列下一成員的位置 this.lowestCount = 0; // 在第一位的成員位置 this.items = {}; // 用來存放的隊列 } enqueue(element) { // 添加隊列成員 進入隊列 } dequeue() { // 刪除隊列成員 離開隊列 } peek() { // 返回隊首的成員 } isEmpty() { // 判斷隊列是否爲空 } clear() { // 將全部的數據初始化 } size() { // 隊列長度 } toString() { // 返回字符串形式的隊列成員 } }
enqueue(element) { this.items[this.count] = element; // 將元素放入隊列 this.count++; // 將計數器加一 }
dequeue() { if (this.isEmpty()) { // 若是是空 return undefined; // 返回未定義 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() { // 判斷長度是否是 0 return this.size() === 0; }
clear() { this.count = 0; // 恢復初始值 this.lowestCount = 0; // 恢復初始值 this.items = {}; // 從新賦值空對象 }
size() { // 隊列長度 等於 整個隊列下一成員的位置 減去 在第一位的成員位置 return this.count - this.lowestCount; }
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; }
相似去醫院看病,急診,會優先通常的門診spa
相似搶凳子游戲,隊列首位相連.net
在添加成員時會判斷優先級,設計
class QueueElement (element, priority){ // 隊列成員類 this.element = element; // 存放成員 this.priority = priority; // 存放優先級 } enqueue(element, priority){ let queueElement = new QueueElement(element, priority); // 添加成員 let added = false; // 是否已添加到隊列 for (let i = 0; i < this.size(); i++){ // 遍歷隊列 if (queueElement.priority < items[i].priority){ // 尋找優先級低的成員,並插入到其以前 // splice start for(let j = this.size(); j > i; j--){ items[j] = items[j-1]; } items[i] = queueElement; // splice end added = true; // 標識符置爲真,表示已經添加 break; // 跳出循環 } } if (!added){ // 若是沒有找到優先級比新添加的成員低的,那麼將其添加到隊尾 items.push(queueElement); } };
在操做時每刪除一個隊列成員就將刪除的這個隊列成員從新添加到隊列中
for (let i = 0; i < number; i++){ queue.enqueue(queue.dequeue()); }