主要分为两种队列:普通队列和双端队列
普通队列
class Queue {
constructor() {
this.count = 0;
this.lowestCount = 0; //记录队首元素位置
this.items = {};
}
enqueue(element) {
this.items[this.count] = element;
this.count++;
}
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];
}
isEmpty() {
return this.count - this.lowestCount === 0;
}
size() {
return this.count - 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;
}
}
双端队列(结合stack+queue)
//双端队列:允许从两端添加和移除元素的特殊队列,结合队列和栈
class Deque {
constructor() {
this.count = 0;
this.lowestCount = 0;
this.items = {};
}
addBack(element) {
//和enqueue一样
this.items[this.count] = element;
this.count++;
}
addFront(element) {
if (this.isEmpty()) {
this.addBack(element);
} else if (this.lowestCount > 0) {
//一个元素已经被从队列的前端移除
this.lowestCount--;
this.items[this.lowestCount] = element;
} else {
//lowestCount=0,所有元素后移一位
for (let i = this.count; i > 0; i--) {
this.items[i] = this.items[i - 1];
}
this.count++;
this.lowestCount = 0;
this.items[0] = element;
}
}
removeFront() {
//和dequeue一样
//先进先出
if (this.isEmpty()) {
return undefined;
}
const result = this.items[this.lowestCount];
delete this.items[this.lowestCount];
this.lowestCount++;
return result;
}
removeBack() {
//pop
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.count - this.lowestCount === 0;
}
size() {
return this.count - this.lowestCount;
}
}
queue应用实例:击鼓传花
//击鼓传花 每次指向的为队首,指向下一个则之前的队首移到队尾,
//达到指定次数num,当前指向(队首)就移除
function hotPotato(elementList, num) {
const queue = new Queue();
const elimitatedList = [];
for (let i = 0; i < elementList.length; i++) {
queue.enqueue(elementList[i]);
}
while (queue.size() > 1) {
for (let i = 0; i < num; i++) {
queue.enqueue(queue.dequeue()); //把队首移除到队尾->指向下一个
}
elimitatedList.push(queue.dequeue()); //移除并记录
}
return {
elimitated: elimitatedList,
winner: queue.dequeue(),
};
}
const names = ["John", "Jack", "Camila", "Ingrid", "Carl"];
const result = hotPotato(names, 7);
console.log(result.elimitated); //['Camila', 'Jack', 'Carl', 'Ingrid']
console.log(result.winner); //John
双端队列应用:回文检查器
//回文检查器
//先处理字符串的大小写和空格等问题,然后入队,利用双端队列的removeFront和removeBack
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("");
//.split(''):将字符串拆分为字符数组,.join(''):将字符数组重新拼接为字符串,没有任何分隔符。
let isEqual = true;
let firstChar, lastChar;
for (let i = 0; i < lowerString.length; i++) {
deque.addBack(lowerString.charAt(i));
}
while (deque.size() > 1 && isEqual) {
firstChar = deque.removeFront();
lastChar = deque.removeBack();
if (firstChar !== lastChar) {
isEqual = false;
}
}
return isEqual;
}
console.log("l e v e l", palindromeChecker("L e v e l"));