#include<stdio.h>
#include<stdlib.h>
#include<conio.h>
#define OVERFLOW -1
typedef int Elemtype;
typedef struct Cnode
{
Elemtype data;
struct Cnode *next;
}CNode;
CNode *joseph;
int Create_clist(CNode *clist ,int n)
{
CNode *p,*q;
int i;
clist=NULL;
for(i=n;i>=1;i--)
{
p=(CNode*)malloc(sizeof(CNode));
if(p==NULL) return OVERFLOW;
p->data=i;
p->next=clist;
clist=p;
if(i==n)
q=p;
}
q->next=clist;
joseph=clist;
return 1;
}
int Joseph(CNode *clist,int m,int n,int k)
{
int i;
CNode *p,*q;
if(m>n) return -1;
if(!Create_clist(clist,n))
return -1;
p=joseph;
for(i=1;i<m;i++)
p=p->next;
while(p)
{
for(i=1;i<k-1;i++)
p=p->next;
q=p->next;
printf("%d",q->data);
if(p->next==p)
p=NULL;
else{
p->next=q->next;
p=p->next;
free(q);
}
}
clist=NULL;
return 1;
}
void main()
{
int m,n,k;
CNode *clist;
clist=NULL;
printf("\n 请输入围坐在圆桌周围的人数n:");
scanf("%d",&n);
printf("请输入第一次开始报数人的位置m:");
scanf("%d",&m);
printf("你希望报数到第几个数的人出列?");
scanf("%d",&k);
Create_clist(clist,n);
printf("出列的顺序如下:\n");
Joseph(clist,m,n,k);
printf("\n");
}
数据结构:约瑟夫环 c语言
最新推荐文章于 2024-09-03 13:57:11 发布
1710

被折叠的 条评论
为什么被折叠?



