求高手看一i下这个堆栈的迷宫问题代码哪错了
#include<stdio.h>#include<malloc.h>
#define n 3
#define m 3
typedef struct node{
int row;
int col;
struct node *next;
}mlink;
mlink *stack;
int MazePath(){
int maze[n+2][m+2],i,j,x1,x2,y1,y2;
mlink *p;
for(i=0;i<m+2;i++){
maze[0][i]=2;
maze[n+1][i]=2;
}
for(i=0;i<n+2;i++){
maze[i][0]=2;
maze[i][n+1]=2;
}
printf("建立迷宫矩阵");
for(i=1;i<=n;i++)
for(j=1;j<=m;j++)
scanf("%d",&maze[i][j]);
printf("输入迷宫入口");
scanf("%d%d",&x1,&y1);
printf("输入迷宫出口");
scanf("%d%d",&x2,&y2);
stack=NULL;
p=(mlink*)malloc(sizeof(mlink));
p->row=x1;
p->col=y1;
p->next=stack;
stack=p;
maze[stack->row][stack->col]=1;
while((stack!=NULL)||((stack->row!=x2)&&(stack->col!=y2))){
if(maze[stack->row][stack->col+1]==0){
p=(mlink*)malloc(sizeof(mlink));
p->row=stack->row;
p->col=stack->col+1;
p->next=stack;
stack=p;
maze[stack->row][stack->col]=1;
}
else if(maze[stack->row][stack->col-1]==0){
p=(mlink*)malloc(sizeof(mlink));
p->row=stack->row;
p->col=stack->col-1;
p->next=stack;
stack=p;
maze[stack->row][stack->col]=1;
}
else if(maze[stack->row+1][stack->col]==0){
p=(mlink*)malloc(sizeof(mlink));
p->row=stack->row+1;
p->col=stack->col;
p->next=stack;
stack=p;
maze[stack->row][stack->col]=1;
}
else if(maze[stack->row-1][stack->col]==0){
p=(mlink*)malloc(sizeof(mlink));
p->row=stack->row-1;
p->col=stack->col;
p->next=stack;
stack=p;
maze[stack->row][stack->col]=1;
}
else{
p=stack;
stack=stack->next;
free(p);
}
}
if(stack=NULL)
return (0);
else
return (1);
}
void main(){
int i;
mlink *p;
i=MazePath();
if(i==1){
p=stack;
printf("其中的一条路线为:\n");
while(p!=NULL){
printf("%d%d\t",p->row,p->col);
p=p->next;
}
}
else
printf("无结果\n");
}
这个程序运行后,给出了这样的错误提示:引用内存非法:0x004011b1指令引用的0x00000000内存,该内存不能为“read".
单步调试后发现问题出在 while((stack!=NULL)||((stack->row!=x2)&&(stack->col!=y2))){上,但不知道怎么改,求教各位大神