【js循环队列】击鼓传花

击鼓传花(HotPotato)游戏,在这个游戏中,孩子们围成一个圆圈,把花尽快地传递给旁边的人。某一时刻传花停止,这个时候花在谁手里,谁就退出圆圈结束游戏。重复这个过程,直到只剩一个孩子(胜者)。

function Queue() {
	this.database = [];
	this.enqueue = function(ele){
		this.database.push(ele);
	};
	this.dequeue = function(){
		return this.database.shift();
	};
	this.front = function(){
		return this.database[0]
	}
	this.back = function(){
		this.database[this.database.length-1];
	};
	this.toString = function(){
		var str = '';
		for (var i = 0; i < this.database.length; i++) {
			str += this.database[i] + '\n';
		}
		return str;
	};
	this.empty = function(){
		return (this.database.length == 0) ? true : false;
	};
	this.size = function(){
		return this.database.length;
	};
}
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());
	    }
	    console.log(queue.dequeue() + '在击鼓传花游戏中被淘汰。');
	}
	return queue.dequeue();
}

let names = ['John','Jack','Camila','Ingrid','Carl'];
let winner = hotPotato(names,7);
console.log('胜利者:' + winner);
击鼓传花输出过程

 

循环链表是一种特殊的链表结构,它与普通链表的区别在于,循环链表的尾节点指向头节点,形成一个闭环。这样一来,可以通过任意节点开始遍历整个链表。 击鼓传花是一个经典的游戏,它可以用循环链表来实现。游戏规则是:一群人围成一个圈,开始时有一个花束(比如一只球)被传递给其中一个人,然后大家按照一定的规则将花束传递下去,当规定的次数到达时,持有花束的人就会被淘汰出局,直到最后只剩下一个人。 以下是使用Python实现循环链表击鼓传花的示例代码: ```python class Node: def __init__(self, data): self.data = data self.next = None class CircularLinkedList: def __init__(self): self.head = None def add_node(self, data): new_node = Node(data) if not self.head: self.head = new_node self.head.next = self.head else: temp = self.head while temp.next != self.head: temp = temp.next temp.next = new_node new_node.next = self.head def remove_node(self, num): if not self.head: return None elif self.head.next == self.head: removed_data = self.head.data self.head = None return removed_data else: temp = self.head while temp.next != self.head: temp = temp.next for _ in range(num - 1): temp = temp.next removed_node = temp.next temp.next = temp.next.next return removed_node.data def play_game(self, num_players, num_passes): for i in range(1, num_players + 1): self.add_node(i) current_player = self.head while self.head.next != self.head: for _ in range(num_passes - 1): current_player = current_player.next removed_player = self.remove_node(num_passes) print(f"Player {removed_player} is out!") current_player = current_player.next winner = self.head.data print(f"The winner is Player {winner}!") # 示例用法 players = 10 # 玩家数量 passes = 3 # 传递次数 game = CircularLinkedList() game.play_game(players, passes) ``` 这段代码中,我们定义了一个`Node`类来表示链表的节点,然后定义了`CircularLinkedList`类来实现循环链表的操作。其中,`add_node`方法用于向链表中添加节点,`remove_node`方法用于移除指定位置的节点,`play_game`方法用于实现击鼓传花游戏的逻辑。 在示例中,我们创建了一个包含10个玩家的循环链表,并设置传递次数为3。最后,通过调用`play_game`方法开始游戏,并输出最后的胜利者。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值