以下代码中使用了哪个 ADT,您如何得出这个结论?我有一种预感,它是一个循环链表,但不知道为什么。
public class Josephus {
private final int M = 3;
private class Soldier {
int num;
Soldier next;
Soldier(int n) {
num = n;
}
}
public void survivor(int N) {
System.out.println("Number of soldiers = " + N);
if (N <= 0)
return;
Soldier first = new Soldier(0);
Soldier curr = first;
for (int i =1; n<N; i++) {
curr.next = new Soldier(i);
curr = curr.next;
}
curr.next = first;
Soldier prev = curr;
int d = 0;
int m = 0;
while (curr != prev) {
if(++m >= M){
d++;
System.out.println("Dead soldier = " + curr.num);
if (curr.num == 0) {
System.out.println("You died as number = " + d);
}
prev.next = curr.next;
m = 0;
} else
prev = curr;
curr = curr.next;
}
System.out.println("Final survivor is = " + curr.num);
}
}