function Queue() {
//屬性
this.items = []
}
//1.將元素加入到隊(duì)列中
Queue.prototype.enqueue = function (element) {
this.items.push(element)
}
//2.從隊(duì)列中刪除前端元素
Queue.prototype.dequeue = function () {
return this.items.shift()
}
//3.查看前端元素
Queue.prototype.front = function () {
return this.items[0]
}
//4.查看隊(duì)列是否為空
Queue.prototype.isEmpty = function () {
return this.items.length === 0
}
//5.查看隊(duì)列中元素的個(gè)數(shù)
Queue.prototype.size = function () {
return this.items.length
}
//6.toString方法
Queue.prototype.toString = function () {
let resultString = ''
for (let i = 0; i < this.items.length; i++) {
resultString += this.items[i] + ''
}
return resultString
}
擊鼓傳花是一個(gè)常見的面試算法題.使用隊(duì)列可以非常方便的實(shí)現(xiàn)最終的結(jié)果.
成都創(chuàng)新互聯(lián)公司長期為上千多家客戶提供的網(wǎng)站建設(shè)服務(wù),團(tuán)隊(duì)從業(yè)經(jīng)驗(yàn)10年,關(guān)注不同地域、不同群體,并針對不同對象提供差異化的產(chǎn)品和服務(wù);打造開放共贏平臺(tái),與合作伙伴共同營造健康的互聯(lián)網(wǎng)生態(tài)環(huán)境。為舒蘭企業(yè)提供專業(yè)的成都網(wǎng)站設(shè)計(jì)、網(wǎng)站建設(shè),舒蘭網(wǎng)站改版等技術(shù)服務(wù)。擁有10年豐富建站經(jīng)驗(yàn)和眾多成功案例,為您定制開發(fā)。
原游戲規(guī)則:
修改游戲規(guī)則:
封裝一個(gè)基于隊(duì)列的函數(shù)
//擊鼓傳花
function paseGame(nameList, num) {
//創(chuàng)建一個(gè)隊(duì)列
let queue = new Queue()
//將所有人依次加入隊(duì)列
for (let i = 0; i < nameList.length; i++) {
queue.enqueue(nameList[i])
}
//開始數(shù)數(shù)字
while (queue.size() > 1) {
//不是num的時(shí)候嗎,重新加入到隊(duì)列的末尾
//num數(shù)字之前的人重新放入到隊(duì)列的末尾
for (let i = 0; i < num - 1; i++) {
queue.enqueue(queue.dequeue())
}
//num對應(yīng)的這個(gè)人直接從隊(duì)列中刪除
queue.dequeue()
}
//獲取剩下的結(jié)果
let endName = queue.front()
console.log(endName);
return nameList.indexOf(endName)
}
paseGame(['lisi', 'zhangsan', 'fgbfd', 'tom', 'jack', 'lisa', 'ez', 'laoshu', 'jikdf', 'dsada', 'poru', 'fjds'], 6)//fgbfd
優(yōu)先級隊(duì)列的特點(diǎn):
優(yōu)先級隊(duì)列主要考慮的問題:
優(yōu)先級隊(duì)列的應(yīng)用:
//封裝優(yōu)先級隊(duì)列
function PriorityQueue() {
//在PriorityQueue重新創(chuàng)建了一個(gè)類
function QueueElemnt(element, priority) {
this.element = element
this.priority = priority
}
//封裝屬性
this.items = []
//1.實(shí)現(xiàn)插入方法
PriorityQueue.prototype.enqueue = function (element, priority) {
//創(chuàng)建QueueElement對象
let queueElemnt = new QueueElemnt(element, priority)
//判斷隊(duì)列是否為空
if (this.items.length === 0) {
this.items.push(queueElemnt)
} else {
let added = false
for (let i = 0; i < this.items.length; i++) {
if (queueElemnt.priority < this.items[i].priority) {
this.items.splice(i, 0, queueElemnt)
added = true
break
}
}
if (!added) {
this.items.push(queueElemnt)
}
}
}
//2.從隊(duì)列中刪除前端元素
PriorityQueue.prototype.dequeue = function () {
return this.items.shift()
}
//3.查看前端元素
PriorityQueue.prototype.front = function () {
return this.items[0]
}
//4.查看隊(duì)列是否為空
PriorityQueue.prototype.isEmpty = function () {
return this.items.length === 0
}
//5.查看隊(duì)列中元素的個(gè)數(shù)
PriorityQueue.prototype.size = function () {
return this.items.length
}
//6.toString方法
PriorityQueue.prototype.toString = function () {
let resultString = ''
for (let i = 0; i < this.items.length; i++) {
resultString += this.items[i] + ''
}
return resultString
}
}
// 測試代碼
let pq = new PriorityQueue()
pq.enqueue('abc', 111)
pq.enqueue('cba', 151)
pq.enqueue('nba', 66)
pq.enqueue('wba', 856)
console.log(pq);