下文用到的隊列和雙端隊列類實現:隊列和雙端隊列類segmentfault
擊鼓傳花:循環隊列code
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(), queue: queue }; }
迴文檢查:雙端隊列隊列
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; }