<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
<script>
// 队列 先进先出
class Queue {
#items = {}
#lowCount = 0
#count = 0
// 出队
dequeue(){
if(this.isEmpty()) return undefined
let res = this.#items[this.#lowCount]
delete this.#items[this.#lowCount]
this.#lowCount++
return res
}
// 入队
enqueue(data){
this.#items[this.#count] = data
this.#count++
}
// 队头
front(){
return this.#items[this.#lowCount]
}
isEmpty(){
return this.size() === 0
}
size() {
return this.#count - this.#lowCount
}
clear() {
this.#items = {}
this.#lowCount = 0
this.#count = 0
}
toString(){
let str = ''
for(let i = this.#lowCount;i<this.#count;i++){
str += `${this.#items[i]} `
}
return str
}
}
// let queue = new Queue()
// console.log(queue.isEmpty())
// console.log(queue.size())
// queue.enqueue('Eric1')
// queue.enqueue('小明2')
// queue.enqueue('tiechui3')
// console.log(queue.dequeue())
// console.log(queue.dequeue())
// console.log(queue.dequeue())
// console.log(queue.dequeue())
game(['kerwin','xiaoming','tiechui','gangdan','guladunzi'],7)
function game(list,num){
let queue = new Queue()
for(let i=0;i<list.length;i++){
queue.enqueue(list[i])
}
while(queue.size() > 1){
for(let i=0;i<num;i++) {
queue.enqueue(queue.dequeue())
}
console.log(queue.dequeue(),'淘汰了')
}
return queue.dequeue()
}
</script>
</body>
</html>