咱們已經學習了棧,隊列和棧很是相似,可是隊列遵循的是先進先出(FIFO)原則的一組有序的項,並從頂部移除元素,可是最新添加的元素必須排在隊列的末尾。在生活中也有隊列的應用,好比咱們在售票處排隊等票,隊頭的人先拿到票,就走了,而新來的人,就必須排在隊偉文明排隊。學習
class Queue { constructor() { this.count = 0; this.lowestCount = 0;//追蹤隊列的第一個元素 this.items = {}; }
enqueue(element) { this.items[this.count] = element; this.count++; }
細節就是要注意到隊列只能在尾部添加元素this
size() { return this.count - this.lowestCount; }; isEmpty() { return this.size() === 0; };
dequeue() { //判斷是否爲空 if (this.isEmpty()) { return 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]; }
clear() { this.items = {}; this.count = 0; this.lowestCount = 0; }
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; }
好的,咱們的隊列到此就建立完畢了。可是,有些小夥伴就有疑問了,仍是排隊情景,假如我已經離開售票廳了,可是我還想再問些簡單問題,直接到前臺詢問,這就是隊首添加元素了,還有隊尾的人忽然有事離開了,這也是刪除元素操做呀,那這個用隊列怎麼實現。恩 ,咱們的前輩就提出了雙端隊列,容許用戶在隊首進行添加和刪除元素的操做,隊尾也是同樣。code
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.items[0] = element; } }
addBack(element) { this.items[this.count] = element; this.count++; }
removeFront() { if (this.isEmpty()) { return undefined; } const result = this.items[this.lowestCount]; delete this.items[this.lowestCount]; this.lowestCount++; return result; }
removeBack() { if (this.isEmpty()) { return undefined; } this.count--; const result = this.items[this.count]; delete this.items[this.count]; return result; }
peekFront() { if (this.isEmpty()) { return undefined; } return this.items[this.lowestCount]; }
peekBack() { if (this.isEmpty()) { return undefined; } return this.items[this.count - 1]; }
情景:孩子們圍城一圈,把花傳遞給身邊的人,某一時刻中止,花在誰手上,誰就推出。重複這個操做,剩下的最後一我的就是勝利者。
代碼實現:隊列
function hotPotato(elementsList, num) { const queue = new Queue(); const elimitatedList = []; for (let i = 0; i < elementsList.length; i++) { queue.enqueue(elementsList[i]); } while (queue.size() > 1) { for (let i = 0; i < num; i++) { queue.enqueue(queue.dequeue()); } elimitatedList.push(queue.dequeue()); } return { eliminated: elimitatedList, winner: queue.dequeue() }; }
檢查一個詞組揮着字符串是否爲迴文。
代碼實現:遊戲
function palindromeChecker(aString) { if ( aString === undefined || aString === null || (aString !== null && aString.length === 0) ) { return false; } const deque = new Deque(); const lowerString = aString.toLocaleLowerCase().split(' ').join(''); let firstChar; let lastChar; for (let i = 0; i < lowerString.length; i++) { deque.addBack(lowerString.charAt(i)); } while (deque.size() > 1) { firstChar = deque.removeFront(); lastChar = deque.removeBack(); if (firstChar !== lastChar) { return false; } } return true; };