用的遍历方法来输出结果,可是结果为空
#include <stdio.h>
#include <stdlib.h>
#include <conio.h>
#include <malloc.h>
#define ERROR 0
#define OK 1
#define FALSE 0
#define TURE 1
#define OVERFLOW -2
#define STACK_INIT_SIZE 80 //栈分配空间的初始容量
#define STACKINCREMENT 10 //栈增加分配空间的量
typedef int SElemType; //自定义数据元素类型为整型
typedef int Status; //自定义函数返回值类型为int
typedef struct
{
SElemType *base;
SElemType *top;
int stacksize; //栈当前的存储空间容量
}SqStack;
Status InitStack(SqStack &S)
{ //创建一个空的顺序栈S
S.base=(SElemType*)malloc(STACK_INIT_SIZE*sizeof(SElemType));
if(!S.base)
{
printf("OVERFLOW");
return ERROR;
}
S.top=S.base;
S.stacksize=STACK_INIT_SIZE;
return OK;
}
Status LengthStack(SqStack &S){
if (S.top==S.base)
{
return ERROR;
}
return (Status)(S.top-S.base);
}
Status Push(SqStack &S,SElemType e)
{ if (S.top-S.base>=S.stacksize)
{
S.base=(SElemType*)realloc(S.base,(S.stacksize+STACKINCREMENT)*sizeof(SElemType));
if (!S.base)
{
printf("OVERFLOW");
return ERROR;
}
S.top=S.base+S.stacksize;
S.stacksize+=STACKINCREMENT;
}
*S.top++=e;
return OK;
}
Status StackTraverse(SqStack &S){
SElemType *p;
if (S.top==S.base){
printf("Stack is Null");
return ERROR;
}
p=S.top;
while (p>S.top)
{
p--;
printf("%d\n",*p);
}
printf("\n");
return OK;
}
int main(){
SqStack S;
int i,n;
SElemType e;
InitStack(S);
LengthStack(S);
printf("input the length of the stack\n");
scanf("%d",&n);
printf("input push elems\n");
for ( i = 0; i < n; i++)
{
scanf("%d",&e);
Push(S,e);
}
printf("the stack is\n");
StackTraverse(S);
return OK;
}