关于循环队列 -> 击鼓传花
function queue(){
let item = [];
this.enqueue = (element) =>{
item.push(element);
}
// 删除第一项
this.dequeue =( ) =>{
return item.shift();
}
// 返回长度
this.size =( element ) =>{
return item.length;
}
this.getitem =function(){
return item;
}
}
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(); //(1) jack (2) camlin
console.log( eliminated +'->被淘汰' );
}
return Queue.dequeue();
}
let names = ['johh','jack','camlin','ing','carl'];
let winner = hotPotato(names,1);
console.log( '赢得人'+winner )