public class P7_1 {
public static void main(String[] args) {
// TODO Auto-generated method stub
LinkList p=null;
LinkList htemp,q;
for(int i=1;i<=41;i++){
p=AddNode(p,i);
}
htemp=p;
while(htemp.next!=null){
htemp=htemp.next;
}
htemp.next=p;
int num=3;
htemp=p;
int j=0;
while(htemp.next!=htemp){
for(int i=1;i<num-1;i++){
htemp=htemp.next;
}
q=htemp.next;
htemp.next=q.next;
htemp=htemp.next;
shuchu(q);
j++;
if(j%5==0){ //计数功能
System.out.printf("\n");
}
}
System.out.printf("轮到第%d个人自杀",htemp.data);
}
static void shuchu(LinkList q){
System.out.printf("轮到第%d个人自杀--->",q.data);
}
static LinkList AddNode(LinkList p,int n){
LinkList q,htemp;
if((q=new LinkList())==null){
return null;
}else{
q.data=n;
q.next=null;
if(p==null){
p=q;
return p;
}else{
htemp=p;
while(htemp.next!=null){
htemp=htemp.next;
}
htemp.next=q;
return p;
}
}
}
static void AllNode(LinkList p){
LinkList htemp;
htemp=p;
do{
System.out.println("数据是:"+htemp.data);
htemp=htemp.next;
}while(htemp!=p);
}
}
class LinkList{
int data;
LinkList next;
}