C语言数据结构josephus问题解决方案 帮我改正下

#include<stdio.h>#include<stdlib.h>structstudent{intnum;structstudent*next;};intlengt... #include<stdio.h>
#include<stdlib.h>
struct student
{
int num;
struct student * next;
};

int length=0;
struct student * create()
{
struct student * p1,* p2,* head;
head=NULL;
p1=p2=(struct student *)malloc(sizeof(struct student));
printf("请输入序号:");
scanf("%d",&p1->num);
while(p1->num!=0)
{
length++;
if(p1->num==1)
head=p1;
else
p2->next=p1;
p2=p1;
p1=(struct student *)malloc(sizeof(struct student));
printf("请输入序号:");
scanf("%d",&p1->num);
};
if(length!=0)
p2->next=head;
else
printf("输入完毕!\n\n");
return head;
}

struct student * find(struct student * head,int s)
{
struct student * p=head;
int i=s;
for(;i>1;i--)
if(p->next!=NULL)p=p->next;
printf("%d ",p->num);
return p;
}

struct student * del(struct student * head,int m)
{
struct student * p1,* p2;
p1=p2=head;
p1=p1->next;
for(;m>1;m--)
{
p2=p1;
p1=p1->next;
}
p2=p1->next;
length--;
printf("%d ",p1->num);
return p1->next;
}

void main()
{
struct student * head;
int n,s,m;
head=create();
printf("请输入开始的序号:");
scanf("%d",&s);
head=find(head,s);
printf("请输入每次轮过的人数:");
scanf("%d",&m);
printf("答案为:");
for(n=length;n>0;n--)
head=del(head,m);
printf("\n");
}

各位大手子帮我看下我是哪里错了.
顺序输入能解出,乱序输入会出现错误答案.
展开
 我来答
笃侠6A
2012-12-15 · TA获得超过3734个赞
知道大有可为答主
回答量:3205
采纳率:75%
帮助的人:3264万
展开全部
给你改了一个,功能划分上有点变化,不知是否符合你的要求:
#include<stdio.h>
#include<stdlib.h>
struct student
{
int num;
struct student * next;
};

int length=0;
void display(struct student *h)
{
int i;
struct student *p=h;
if(length==0){
printf("链表为空。\n");
return;
}
for(i=0; i<length; i++)
{
printf("%d ", p->num);
p=p->next;
}
printf("\n");
}

struct student * create()
{
struct student *p1, *p2, *head=0;
int n;
while(1)
{
printf("请输入序号:");
scanf("%d",&n);
if(n==0) break;
p1=(struct student *)malloc(sizeof(struct student));
if(!p1) break;
length++;
p1->num=n;
if(length==1)
head=p1, p2=p1;
else
p2->next=p1, p2=p1;
}//while(1)
if(length!=0)
p2->next=head;
printf("输入完毕!\n\n");
return head;
}

//从序号为begin的人开始报数,报到step的人出列
void outring(struct student *h, int begin, int step)
{
struct student *p=h, *q;
int c, i;
while(p->num!=begin) p=p->next;
for(i=0; i<length; i++)
{
for( c=1,q=p; c<step; q=p, p=p->next, c++); //报数
q->next=p->next;
q=p;
printf("%d ", q->num);
p=p->next;
free((struct student*)q);
}
printf("\n");
}

void main()
{
struct student * head;
int s,m;
head=create();
display(head);
printf("请输入开始的序号:");
scanf("%d",&s);
printf("请输入每次轮过的人数:");
scanf("%d",&m);
printf("答案为:");
outring(head,s,m);
}
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式