我用的是类,问一下这是哪里出了问题
#include<stdio.h>
#include<stdlib.h>
#define OVERFLOW -1
#define SUCCESS 1
typedef struct{
int x;
int y;
}Pos;
class Stack{
typedef struct{
Pos data;
struct SNode*next;
}SNode,*PSNode;
PSNode head;
int length;
public:
Stack(){
head=NULL;
length=0;
}
int Push(Pos data){
PSNode p=new SNode;
if(!p)return OVERFLOW;
p->data=data;
p->next=head;
head=p;
length++;
return SUCCESS;
}
int GetTop(Pos&data)
{
if(!head)return OVERFLOW;
data=head->data;
return SUCCESS;
}
int Pop(Pos&data){
if(!head)return OVERFLOW;
PSNode p;
p=head;
data=head->data;
head=p->next;
delete p;
length--;
return SUCCESS;
}
bool StackEmpty()
{
if(length)return false;
return true;
}
void clear()
{
PSNode p=head;
while(p)
{
head=p->next;
delete p;
p=head;
}
}
~Stack()
{
clear();
}
};
int map[10][10]={
{8,8,8,8,8,8,8,8,8,8},
{8,0,0,8,0,0,0,8,0,8},
{8,0,0,8,0,0,0,8,0,8},
{8,0,0,0,0,8,8,0,0,8},
{8,0,8,8,8,0,0,0,0,8},
{8,0,0,0,8,0,0,0,0,8},
{8,0,8,0,0,0,8,0,0,8},
{8,0,8,8,8,0,8,8,8,8},
{8,8,0,0,0,0,0,8,0,8},
{8,8,8,8,8,8,8,8,8,8}
};
bool GetNext(Pos pos,Pos&nextpos)
{
if(map[pos.x][pos.y]==0&&map[pos.x+1][pos.y]==0)//判断南边是否有路
{
nextpos.x=pos.x+1;nextpos.y=pos.y;map[pos.x][pos.y]=1;return true;
}
if(map[pos.x][pos.y]<=1&&map[pos.x][pos.y+1]==0)//判断东边是否有路
{
nextpos.x=pos.x;nextpos.y=pos.y+1;map[pos.x][pos.y]=2;return true;
}
if(map[pos.x][pos.y]<=2&&map[pos.x][pos.y-1]==0)//判断西边是否有路
{
nextpos.x=pos.x;nextpos.y=pos.y-1;map[pos.x][pos.y]=3;return true;
}
if(map[pos.x][pos.y]<=3&&map[pos.x-1][pos.y]==0)//判断北边是否有路
{
nextpos.x=pos.x-1;nextpos.y=pos.y;map[pos.x][pos.y]=4;return true;
}
return false;
}
int main()
{
Stack s;
Pos startpos,curpos,endpos,nextpos;
startpos.x=1;startpos.y=1;
endpos.x=8;endpos.y=8;
s.Push(startpos);
printf("迷宫:\n");
for(int i=0;i<10;i++)
{
for(int j=0;j<10;j++)
{
if(j==9)printf("\n");
printf("%d",map[i][j]);
}
}
printf("起点坐标:(%d,%d)\n",startpos.x,startpos.y);
printf("终点坐标:(%d,%d)\n",endpos.x,endpos.y);
curpos.x=startpos.x;curpos.y=startpos.y;
bool found=false;
while(s.GetTop(curpos)&&!found)
{
if(curpos.x==endpos.x&&curpos.y==endpos.y){
found=true;break;
}
if(GetNext(curpos,nextpos))//如果能找到下一步
{
s.Push(nextpos);
}
else {
if(s.StackEmpty())break;//栈是否为空
s.Pop(curpos);
}
}
if(found)
{
printf("路径逆序为:\n");
while(!s.StackEmpty())
{
s.Pop(curpos);//将栈中元素逆序出栈
printf("(%d,%d)",curpos.x,curpos.y);
}
}
else printf("找不到出口\n");
}
编译结果总是显示