谁帮我看一下这个猴王问题

来源:百度知道 编辑:UC知道 时间:2024/06/21 14:49:28
自己写的一段代码,实在找不出问题了,但是就是得不出期望的结果,希望哪位高手能帮我看看,希望在我的基础上找问题而不是另起炉灶,我只要知道自己的问题
#include <stdio.h>
#include <stdlib.h>
typedef struct Cnode
{
int num;
int flag;
struct Cnode *next;
}CNode;
CNode* creat_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 NULL;
p->num=i;
p->next=clist;
clist=p;
if(i==n)
q=p;
}
q->next=clist;
return clist;
}
void Rabbit(CNode *clist)
{
CNode *p;
int i;
int m=1;
p=clist;
do
{
p->flag=1;
p=p->next;
}while(p!=clist);
p->flag=0;
while(m<=999)
{
i=m;
while(i>=0)
{
p=p->next;
i--;
}
p->flag=0;
m++;
}
p=clist;
do
{

//你能不能把你要实现的功能贴出来。
//我看你的代码,但是我不知道你的功能是要实现什么?
/*
if(p->flag=1)
printf("第%d个洞可能有兔子\n",p->num);

=应该为==,这是很容易犯的一个错误。

另外我做了一些小小的修改。
*/
#include <stdio.h>
#include <stdlib.h>

typedef struct Cnode
{
int num;//1~n
int flag;//1表示有兔子
struct Cnode *next;
}CNode;

//创建链表
CNode* creat_clist(CNode *clist,int n)
{
CNode *head=NULL,*last=NULL;//头尾指针
CNode *p=NULL;//临时使用
int i;
for(i=1;i<=n;i++)
{
p=(CNode *)malloc(sizeof(CNode));
p->num=i;
p->next=NULL;
if(head){//链表非空
last->next=p;
last=p;//尾指针移到最后位置
}
else//链表为空
{
head=p;
last=p;
}
}
last->next=head;//首尾串起来
clist=head;
return clist;
}

//找兔子
void Rabbit(CNode *clist)
{
CNode *p;
int i;
int