隊列钧萍,和棧有點類似窄俏,但是又不太一樣狈邑,隊列遵循 先進先出 的原則。
一蚤认、模擬實現(xiàn)隊列
首先米苹,為隊列聲明一些方法:
-
enqueue(element)
:向隊列尾部添加一個或者多個新的項。 -
dequeue()
:移除隊列的第一(即排在隊列最前面的)項砰琢,并返回被移除的元素蘸嘶。 -
front()
:返回隊列中第一個元素——最先被添加,也將是最先被移除的元素陪汽。隊列不做任何改動训唱。 -
isEmpty()
:如果隊列中不包含任何元素,返回true
挚冤,否則返回false
况增。 -
size()
:返回隊列中的元素個數(shù),和數(shù)組的length
屬性類似训挡。
然后澳骤,嘗試實現(xiàn)這些方法:
function Fun() {
const items = [];
// 1. 元素入隊列
this.enqueue = function(element) {
items.push(element);
};
// 2. 元素出隊列
this.dequeue = function() {
return items.shift();
};
// 3. 查看隊列頂部元素
this.front = function() {
return items[0];
};
// 4. 判斷隊列是否為空
this.isEmpty = function() {
return items.length === 0;
};
// 5. 查看整個隊列長度
this.size = function() {
return items.length;
};
// 6. 查看整個隊列
this.print = function() {
console.log(items);
}
};
let fun = new Fun();
fun.enqueue('1'); // [ '1' ]
fun.enqueue('2'); // [ '1', '2' ]
fun.dequeue(); // [ '2' ]
fun.dequeue(); // [ ]
fun.print(); // []
最后,推薦幾個案例可以進一步了解隊列:
二为肮、優(yōu)先隊列
示例:
function PriorityQueue() {
// 1. 定義空數(shù)組
const items = [];
// 2. 定義隊列
function QueueElement(element, priority) {
this.element = element;
this.priority = priority;
}
// 3. 實現(xiàn)入隊列方式
this.enqueue = function(element, priority) {
let queueElement = new QueueElement(element, priority);
let added = false;
for (let i = 0; i < items.length; i++) {
// 如果可以插隊,那么就插入到隊列肤京,且終止本次循環(huán)(減少時間浪費以及重復(fù)添加)
if (queueElement.priority < items[i].priority) {
items.splice(i, 0, queueElement);
added = true;
break;
}
}
if (!added) {
items.push(queueElement);
}
};
// 4. 實現(xiàn)隊列打印
this.print = function() {
items.forEach((ele) => {
console.log(`${ele.element} ${ele.priority}`);
})
};
// 5. 其他方法和默認的Queue實現(xiàn)相同
}
const priorityQueue = new PriorityQueue();
priorityQueue.enqueue('zhangsan', 1);
priorityQueue.enqueue('JavaScript', 2);
priorityQueue.enqueue('張三', 1)
priorityQueue.print();
// zhangsan 1
// 張三 1
// JavaScript 2
三颊艳、擊鼓傳花
示例:
/**
* @name 隊列模擬
*/
function Queue() {
const items = [];
// 1. 元素入隊列
this.enqueue = function(element) {
items.push(element);
};
// 2. 元素出隊列
this.dequeue = function() {
return items.shift();
};
// 3. 查看隊列頂部元素
this.front = function() {
return items[0];
};
// 4. 判斷隊列是否為空
this.isEmpty = function() {
return items.length === 0;
};
// 5. 查看整個隊列長度
this.size = function() {
return items.length;
};
// 6. 查看整個隊列
this.print = function() {
console.log(items);
}
};
/**
* @name 擊鼓傳花
* @param {*} nameList 人名
* @param {*} num 淘汰的位置
*/
function hotPotato(nameList, num) {
let queue = new Queue();
for (let i = 0; i < nameList.length; 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();
}
const names = ['name1', 'name2', 'name3', 'name4', 'name5'];
const winner = hotPotato(names, 7);
console.log('贏家是:' + winner);
// 淘汰了:name3
// 淘汰了:name2
// 淘汰了:name5
// 淘汰了:name4
// 贏家是:name1
在這次游戲中,淘汰順序是:
- name3
- name2
- name5
- name4
最后剩下 name1
忘分,退出循環(huán)棋枕,我們將其推出棧(此時棧為空)。
當(dāng)然饭庞,這里我們固定了傳遞進來的數(shù)字為 7戒悠,如果我們通過隨機來指定一個,那么應(yīng)該是:
// 隨機式擊鼓傳花
// ...主體代碼如上
const names = ['name1', 'name2', 'name3', 'name4', 'name5'];
const winner = hotPotato(names, Math.floor(Math.random() * 10 + 1));
console.log('贏家是:' + winner);
// 淘汰了:name1
// 淘汰了:name4
// 淘汰了:name2
// 淘汰了:name3
// 贏家是:name5
// 現(xiàn)在淘汰的位置不固定了
不折騰的前端舟山,和咸魚有什么區(qū)別绸狐! ------冷楓1211