【JS数据结构】队列的应用 击鼓传花

下午有点晕…不会也要羊了吧,进不了决赛圈了吗卧槽!

直接上代码

function Queue() {
  this.items = ['Lili','Lucy','Tom','Lilei','Why'];
  Queue.prototype.enqueue = function (elment) {
    this.items.push(elment);
  };
  Queue.prototype.dequeue = function () {
    this.items.shift();
  };
  Queue.prototype.front = function () {
    return this.items[0];
  };
  Queue.prototype.isEmpty = function () {
    return this.items.length == 0;
  };
  Queue.prototype.size = function () {
    return this.items.length;
  };
  Queue.prototype.toString = function () {
    var str = '';
    for (let i = 0; i < this.items.length; i++) {
      str += this.items[i] + '';
    }
    return str;
  };
}
//击鼓传花   给定一个数字 每次循环 从下标0开始 每次+1  +到这个数字  该数字对应的下标出队
//队列先进先出  也就是从下标0的那个出去 那么也就是出队 -> 入队 循环  其中数字对应的那个人只出队....循环直到剩下最后一个
//这是什么 一个函数?  传入一个队列 与 数字 最终return 出那个数字
var queues = new Queue();
console.log( queues);
function pass(queue, item) {
    while(queue.size()!==1){
        for(let i=1;i<item;i++){
            let res1=queue.front()
            console.log( res1);
            queue.dequeue()
            queue.enqueue(res1)
        }
        queue.dequeue()
    }
    return queue.front()
}

const res= pass(queues,3)
console.log(res);

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值