这个程序哪里错?
来源:百度知道 编辑:UC知道 时间:2024/06/06 04:52:21
#define STACK_INIT_SIZE 100
#define STACKINCREMENT 10
#include <stdio.h>
typedef struct
{
int *base;
int *top;
int stacksize;
}SqStack;
SqStack *S;
void InitStack(SqStack *S)
{
S->base=(int *)malloc(STACK_INIT_SIZE*sizeof(int));
if(!S->base)
exit(0);
S->top=S->base;
S->stacksize=STACK_INIT_SIZE;
}
void 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)
exit(0);
S->top=S->base+S->stacksize;
S->stacksize+=STACKINCREMENT;
}
* S->top++=e;
}
int Pop(SqStack *S)
{
int e;
if(S->top==S->base)
return 0;
e=* --S->top;
return e;
#define STACKINCREMENT 10
#include <stdio.h>
typedef struct
{
int *base;
int *top;
int stacksize;
}SqStack;
SqStack *S;
void InitStack(SqStack *S)
{
S->base=(int *)malloc(STACK_INIT_SIZE*sizeof(int));
if(!S->base)
exit(0);
S->top=S->base;
S->stacksize=STACK_INIT_SIZE;
}
void 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)
exit(0);
S->top=S->base+S->stacksize;
S->stacksize+=STACKINCREMENT;
}
* S->top++=e;
}
int Pop(SqStack *S)
{
int e;
if(S->top==S->base)
return 0;
e=* --S->top;
return e;
你这里是一个10-8进制转换程序
这是你的程序:
#define STACK_INIT_SIZE 100
#define STACKINCREMENT 10
#include <stdio.h>
typedef struct
{
int *base;
int *top;
int stacksize;
}SqStack;
SqStack *S; /* 1. 问题在此,你只是申明了一个指针变量, */
/* 不是结构体变量,没有结构体变量分配空间 */
void InitStack(SqStack *S)
{
S->base=(int *)malloc(STACK_INIT_SIZE*sizeof(int));
/* 在这里却直接向指针所指向的结构体的base域存放数据,所以有问题 */
if(!S->base)
exit(0);
S->top=S->base;