package linkedList;
public class Joseph {
public static void main(String[] args) {
CircleLinkList cir = new CircleLinkList();
cir.addBoy(15);
cir.getAllInfo();
cir.countBoy(1, 2, 15);
}
}
// 创建环形的单向链表
class CircleLinkList {
// 创建一个first检点
private Boy first = null;
// 添加小孩
public void addBoy(int nums) {
if (nums < 1) {
System.out.println("输入不符合规范");
return;
}
Boy curBoy = null;
for (int i = 1; i <= nums; i++) {
Boy boy = new Boy(i);
// if is the first boy?
if (i == 1) {
first = boy;
first.setNext(first);// 成环
curBoy = first;
} else {
// 环状链表加入一个节点
curBoy.setNext(boy);
boy.setNext(first);
curBoy = boy;
}
}
}
public void getAllInfo() {
if (first.getNext() == null) {
System.out.println("链表为空");
return;
}
Boy curBoy = first;
while (true) {
System.out.printf("小孩编号 %d \n", curB
约瑟夫问题(Java环形列表实现)
最新推荐文章于 2023-06-08 00:15:24 发布