关于栈的问题,帮帮忙
我修改一下题目我想问一下:为什么我这个程序在初始化那里无法成功呢?
#include<stdio.h>
#include<stdlib.h>
#define STACK_INIT_SIZE 100
#define STACKINCREMENT 2
#define OK 1
#define ERROR 0
#define TRUE 1
#define FALSE 0
typedef struct
{
int x ;
int y ;
int di;
}PosType;
typedef struct
{
PosType *base;
PosType *top;
int stacksize;
}SqStack;
int InitStack(SqStack *S)
{
S->base =(PosType *)malloc(STACK_INIT_SIZE * sizeof(PosType));
if(!S->base) exit(0);
S->top=S->base;
S->stacksize=STACK_INIT_SIZE;
return OK;
}
int DestroyStack(SqStack * S)
{
free(S->base);
S->base=NULL;
S->top=NULL;
S->stacksize=0;
return OK;
}
int ClearStack(SqStack *S)
{
S->top=S->base;
return OK;
}
int StackEmpty(SqStack *S)
{
if(S->top==S->base)return TRUE;
else return FALSE;
}
int GetTop(SqStack *S,PosType *e)
{
if(S->top==S->base)
return ERROR;
*e=*(S->top-1);
return OK;
}
int push(SqStack *S,PosType* e)
{
if(S->top - S->base >= S->stacksize)
{
S->base=(PosType *)realloc(S->base,(S->stacksize+STACKINCREMENT)* sizeof(PosType));
if(!S->base) exit(-1);
S->top=S->base+S->stacksize;
S->stacksize +=STACKINCREMENT;
}
*S->top++=*e;
return OK;
}
int pop (SqStack *S,PosType *e)
{
if(S->top==S->base)
return ERROR;
*e=*--S->top;
return OK;
}
int Migong(int *p,SqStack *S,PosType *start,PosType *end,int e,int r)
{
PosType a,*b;
a =*start;
b=&a;
while(!StackEmpty(S))
{
if( *(p +( a.x * e )+a.y) ==0 )
{
push(S, b);
if(a.x == end->x && end->y ==a.y ) return TRUE;
a.x=a.x+1;
a.di=1;
}
else
{
if(!StackEmpty(S))
{
pop(S,b);
while(a.di == 4 && !StackEmpty(S))
{
pop(S,b);
}
if(a.di<4)
{
a.di++;
push(S,b);
switch(a.di)
{
case 2:a.y=a.y+1;break;
case 3:a.x=a.x-1;break;
case 4:a.y=a.y-1;break;
}
}
}
}
return FALSE;
}
}
void main()
{
int a,b,c,d,i,j,r1,r2;
int *p;
int mg[10][10];
PosType *m,*n;
SqStack *S;
InitStack(S);
printf("请输入迷宫的长和宽\n");
scanf("%d,%d",&r1,&r2);
printf("请输入迷宫的图案(通:0;不通:1)\n");
for(i=0;i<r1;i++)
for(j=0;j<r2;j++)
scanf("%d",mg[i][j]);
p=&mg[0][0];
printf("请输入迷宫的入口,出口((a,b))\n");
scanf("%d,%d",&a,&b,&c,&d);
m->x=a-1;
m->y=b-1;
n->x=c-1;
n->y=d-1;
m->di=1;
if(!Migong(p,S,m,n,r1,r2))
printf("走不通\n");
else
{while(S->base != S->top)//直到走完所有的方向
{
pop(S,m);
if(m->di <= 4)
{
m->di++;
Migong(p,S,m,n,r1,r2);
}
}
}
}
[ 本帖最后由 樱花自在 于 2013-7-11 00:05 编辑 ]