约瑟夫环问题 设计一个带头结点的循环单链表类,实现约瑟夫环问题; 问题描述:设编号为1,2,…,n(n>0)个

2025-05-17 23:21:40
推荐回答(1个)
回答(1):

#include

struct node{
int data;
struct node *next;
}node,*list,*p,*r;

void JOSEPHU(int n,int k,int m)
{
int i,j;
list=NULL;
for(i=1;i<=n;i++)
{
p=(struct node*)malloc(sizeof(node));
p->data=i;
if(list==NULL)
list=p;
else
r->next=p;
r=p;
}
p->next=list; /*建立一个循环链表*/

p=list;
for(i=1;i<=n+1;i++)
{
printf("%d ",p->data);
p=p->next;
}
printf("\n"); /*打印链表,并检查循环链表是不输入正确*/
p=list;
i=1;
while(p&&i { r=p;
p=p->next;
++i;
}
for(i=1;i {
for(j=1;j { r=p;
p=p->next;
}
printf("The out=%d\n",p->data);
r->next=p->next;
}
}
void main()
{
int x, y, z;
printf("input the lenth n\n");/*n,k,m分别代表总的人数,第一个报数的人,间隔的人数*/
scanf("%d",&x);
printf("input the start k\n");
scanf("%d",&y);
printf("input the m\n");
scanf("%d",&z);
JOSEPHU(x,y,z);
}