创建队列
let itemsfunction Queue { this.enqueue = function(element){ items.push(element) } this.dequeue = function(){ return items.shift() } this.front = function(){ return items[0] } this.isEmpty = function(){ return items.length === 0 } this.size = function(){ return items.length } this.print = function(){ console.log(items.toString()) }}
使用ES6改造
let Queue = (function(){ const items = new WeakMap() class Queue { constructor(){ items.set(this,[]) } enqueue(element){ let q = items.get(this) q.push(element) } dequeue(){ let q = items.get(this) let r = q.shift() return r } } return Queue})()
最小优先队列
function PriorityQueue(){ let items = [] function QueueElement(element,priority){ this.element = element this.priority = priority } let added = false; this.enqueue = function(element,priority){ let queueElement = new QueueElement(element,priority) for(let i = 0; i < items.length; i++){ if(queueElement.priority < items[i].priority){ items.splice(i,0,queueElement) added =true break } } if(!added){ items.push(queueElement) } } this.print = function(){ console.log(items) }}
双端队列
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]; } isEmpty() { return this.size() === 0; } clear() { this.items = {}; this.count = 0; this.lowestCount = 0; } 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; }}
循环队列
function hotPotato(nameList,num){ let queue = new Queue() for(let i = 0; i < nameList; i++){ queue.enqueue(nameList[i]) } let eliminated = ''; while(queue.size() > 1){ for(let i = 0; i < num; i++){ queue.enqueue(queue.dequeue) } eliminated = queue.dequeue() console.log(eliminated + '在击鼓传花游戏中被淘汰') } return 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}