数据结构课程设计遇到问题了 望大家帮忙看看 回答好的加分 急急急!!!

猴子选大王问题:#include<stdio.h>#include<stdlib.h>typedefstructmonkey{intnum;structmonkey*ne... 猴子选大王问题:
#include <stdio.h>
#include <stdlib.h>

typedef struct monkey
{
int num;
struct monkey *next;
} Monkey,*LINK;

/*创建循环链表,容纳M个猴子。返回指向链表头结点的指针*/
int createList(int M)
{ LINK p,head1,p2;
int i;
head1=p=p2=(LINK)malloc(sizeof(Monkey));
for(i=1;i<M;i++)
{
p=(LINK)malloc(sizeof(Monkey));
p2->next=p;
p2=p;
}
p2->next=head1;
p=head1;
printf("对猴子进行编号!\n");
for(i=1;i<=M;i++)
{
p->num=i;
printf("%d号猴子:%d\n",p->num,p->num);
p=p->next;
}
return 1;
}

/*形成循环链表*/

/*从headP指向的循环链表中选大王,数到N的猴子淘汰,将依次淘汰出来的猴子插入到headPtr2指向的链表中*/
void selectKing(LINK head,int N,int M)/*N>=2*/
{
LINK p,p2,head2=NULL;
int i;
i=0;
p=head2;//p指向第一个结点

while(1)
{
i++;
printf("%d号猴子报:%d\n",p->num,i);
if(p->next==p)
break;//此为while循环的出口

if(i==N)//if语句中是删除结点的过程
{
i=0;
printf("%d号猴被淘汰\n",p->num);
printf("\n");
p2->next=p->next;//在此删除结点p
p=p2->next;//p指向它的下一个结点
continue;
}
else
{
if(i==N-1)
p2=p;//保存将要退出结点的前一个结点(存到p2中)
p=p->next;
printf("选出的猴子大王的编号为:%d\n",p->num);//最后剩下的结点就是获胜的结点

}

}
}

int main()
{
LINK head=NULL;
int M,N;

printf("input the amount of monkeys:");
scanf("%d",&M); /*猴子个数*/
printf("input the count number:");
scanf("%d",&N); /*count=3,表示每次数到3的猴子出局*/

head=createList(M);/*创建循环链表*/

selectKing(head,N,M);/*选大王*/
return 0;
}
程序没有报错,但是就是无法正常输出结果。可能是逻辑错误,望高手帮忙!
我还有一个迷宫程序,也又一些错误,望大家指点指点,加分!
展开
linzhonghou
2009-06-18 · TA获得超过274个赞
知道小有建树答主
回答量:143
采纳率:0%
帮助的人:162万
展开全部
给你做了一点改动,仔细看看就知道了。
#include <stdio.h>
#include <stdlib.h>

typedef struct monkey
{
int num;
struct monkey *next;
} Monkey,*LINK;

/*创建循环链表,容纳M个猴子。返回指向链表头结点的指针*/
LINK createList(int M)
{ LINK p,head1,p2;
int i;
head1=p=p2=(LINK)malloc(sizeof(Monkey));
for(i=1;i<M;i++)
{
p=(LINK)malloc(sizeof(Monkey));
p2->next=p;
p2=p;
}
p2->next=head1;
p=head1;
printf("对猴子进行编号!\n");
for(i=1;i<=M;i++)
{
p->num=i;
printf("%d号猴子:%d\n",p->num,p->num);
p=p->next;
}
return head1;
}

/*形成循环链表*/

/*从headP指向的循环链表中选大王,数到N的猴子淘汰,将依次淘汰出来的猴子插入到headPtr2指向的链表中*/
void selectKing(LINK head,int N,int M)/*N>=2*/
{
LINK p,p2,head2=NULL;
int i;
i=0;
p=head;//p指向第一个结点

while(1)
{
i++;
printf("%d号猴子报:%d\n",p->num,i);
if(p->next==p)
break;//此为while循环的出口

if(i==N)//if语句中是删除结点的过程
{
i=0;
printf("%d号猴被淘汰\n",p->num);
printf("\n");
p2->next=p->next;//在此删除结点p
p=p2->next;//p指向它的下一个结点
continue;
}
else
{
if(i==N-1)
p2=p;//保存将要退出结点的前一个结点(存到p2中)
p=p->next;
printf("选出的猴子大王的编号为:%d\n",p->num);//最后剩下的结点就是获胜的结点

}

}
}

int main()
{
LINK head=NULL;
int M,N;

printf("input the amount of monkeys:");
scanf("%d",&M); /*猴子个数*/
printf("input the count number:");
scanf("%d",&N); /*count=3,表示每次数到3的猴子出局*/

head=createList(M);/*创建循环链表*/

selectKing(head,N,M);/*选大王*/
return 0;
}
BJ华夏艺匠
2024-08-11 广告
专业模型的生产商有很多,北京华夏艺匠模型科技有限公司值得了解一下。北京华夏艺匠模型科技有限公司 ,拥有十余年沙盘模型制作经验,公司占地约2119平米,拥有一支专业工业、机械设备、效果图设计、模型制作团队。公司目前总部设在北京、上海、两个生产... 点击进入详情页
本回答由BJ华夏艺匠提供
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

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

类别

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

说明

0/200

提交
取消

辅 助

模 式