一道数据结构题:#include #include #define STACK_INIT_SIZE 100#define STACKINCREMENT 10#define OK 1#define ERROR 0typedef struct{int *base;int *top;int stacksize;}SqStack;int InitStack(SqStack *S){S->base=(int *)malloc(STACK_INIT_SIZE*sizeof(i

来源:学生作业帮助网 编辑:六六作业网 时间:2024/12/23 01:51:11
一道数据结构题:#include#include#defineSTACK_INIT_SIZE100#defineSTACKINCREMENT10#defineOK1#defineERROR0typed

一道数据结构题:#include #include #define STACK_INIT_SIZE 100#define STACKINCREMENT 10#define OK 1#define ERROR 0typedef struct{int *base;int *top;int stacksize;}SqStack;int InitStack(SqStack *S){S->base=(int *)malloc(STACK_INIT_SIZE*sizeof(i
一道数据结构题:
#include
#include
#define STACK_INIT_SIZE 100
#define STACKINCREMENT 10
#define OK 1
#define ERROR 0
typedef struct{
int *base;
int *top;
int stacksize;
}SqStack;
int InitStack(SqStack *S)
{
S->base=(int *)malloc(STACK_INIT_SIZE*sizeof(int));
if(!S->base) return ERROR;
S->top=S->base;
S->stacksize=STACK_INIT_SIZE;
return OK;
}
int Push(SqStack *S,int e)
{
if(S->top-S->base>=S->stacksize)
{S->base=(int *)realloc(S->base,(S->stacksize+STACKINCREMENT)*sizeof(int));
if(!S->base) return ERROR;
S->top=S->base+S->stacksize;
S->stacksize+=STACKINCREMENT;
}
*(S->top)=e;
S->top++;
return OK;
}
int Pop(SqStack *S,int *e)
{
if(S->top==S->base) return ERROR;
*e=*(S->top-1);
return OK;
}
int GetTop(SqStack S,int *e)
{
if(S.top==S.base) return ERROR;
S.top--;
*e=*(S.top);
return OK;
}
void main()
{
SqStack S;
int i,j,*p;
i=2,j=4;
InitStack(&S);
Push(&S,i);
Push(&S,j);
GetTop(S,p);
printf("%d",*p);
}
为什么运行有错
用VC++是这样的:

一道数据结构题:#include #include #define STACK_INIT_SIZE 100#define STACKINCREMENT 10#define OK 1#define ERROR 0typedef struct{int *base;int *top;int stacksize;}SqStack;int InitStack(SqStack *S){S->base=(int *)malloc(STACK_INIT_SIZE*sizeof(i
我试了一下,没有问题的,程序运行结果是4,就是栈顶元素的值
手上没有VC++,我是用GCC编译的