农夫过河问题??C语言 运行无结果
#include<stdio.h>
#include<stdlib.h>
struct Node;
typedef struct Node *PNode;
struct Node
{
int info;
PNode link;
};
struct LinkQueue
{
PNode f;
PNode r;
};
typedef struct LinkQueue *PLinkQueue;
PLinkQueue Create_EmptyLinkQueue(void)
{
PLinkQueue p;
p=(PLinkQueue)malloc(sizeof(LinkQueue));
if(p!=NULL)
{
p->f=NULL;
p->r=NULL;
}
else printf("Out of space!\n");
return p;
}
void Enter_LinkQueue(PLinkQueue q,int x)
{
PNode p=(PNode)malloc(sizeof(Node));
if(p==NULL) printf("Out of space!\n");
else
{
p->info=x;
p->link=NULL;
if(q->f==NULL) q->f=p;
else q->r->link=p;
q->r=p;
}
}
void Delete_LinkQueue(PLinkQueue q)
{
PNode p;
if(q->f==NULL) printf("Empty queue.\n");
else
{
p=q->f;
q->f=q->f->link;
free(p);
}
}
int Front_LinkQueue(PLinkQueue p)
{
if(p->f==NULL) printf("Empty queue.\n");
else return(p->f->info);
}
int IsEmpty_LinkQueue(PLinkQueue p)
{
return(p->f!=NULL);
}
int farmer(int location)
{
return(0!=location&0x08);
}
int wolf(int location)
{
return(0!=location&0x04);
}
int cabbage(int location)
{
return(0!=location&0x02);
}
int goat(int location)
{
return(0!=location&0x01);
}
int safe(int location)
{
if(goat(location)==wolf(location)&&goat(location)!=farmer(location))
return 0;
if(cabbage(location)==goat(location)&&cabbage(location)!=farmer(location))
return 0;
return 1;
}
void FarmerProblem()
{
int i,movers,location,newlocation;
int route[16];
PLinkQueue moveto;
moveto=Create_EmptyLinkQueue();
Enter_LinkQueue(moveto,0x00);
printf("当前入队状态是%d\n",0x00);
for(i=0;i<16;i++)
route[i]=-1;
route[0]=0;
while(!IsEmpty_LinkQueue(moveto)&&(route[15]==-1))
{
location=Front_LinkQueue(moveto);
printf("当前出队的状态是%d\n",location);
Delete_LinkQueue(moveto);
for(movers=1;movers<=8;movers<<=1)
{
if((0!=(location&&0x08))==(0!=(location&&movers)))
{
newlocation=location^(0x08|movers);
if(safe(newlocation)&&route[newlocation]==-1)
{
route[newlocation]=location;
Enter_LinkQueue(moveto,newlocation);
printf("当前入队的状态是%d\n",newlocation);
}
}
}
}
if(route[15]!=-1)
{
printf("反路径为:\n");
for (location=15;location>=0;location=route[location])
{
printf("The location is : %d\n", location);
if (location == 0) exit(0);
}
}
else printf("问题无解.\n");
}
int main()
{
FarmerProblem();
return 0;
}
运行显示"当前入队状态是0,问题无解."
求教???为什么运行无结果???谢谢!