#include<stdio.h>
#include<stdlib.h>
#define max 5
#define creat 10
typedef int selemtype ;
typedef int Status;
#define OVERFLOW -2
#define OK 1
#define ERROR 0
typedef struct {
selemtype * base;
selemtype* top;
int staticsize;
}Sqstatic;
Status Initstack(Sqstatic& S) {
S.base = (selemtype*)malloc(max * sizeof(selemtype));
if (!S.base)exit(OVERFLOW);
S.top = S.base;
S.staticsize = max;
return OK;
}
Status Push(Sqstatic& S, selemtype &e)
{
if (S.top - S.base >= S.staticsize) {
S.base = (selemtype*)realloc(S.base, (S.staticsize + creat) * sizeof(selemtype));
if (!S.base)exit(OVERFLOW);
S.top = S.base + S.staticsize;
S.staticsize += creat;
}
*S.top++ = e;
return OK;
}
Status Pop(Sqstatic &S, selemtype &e) {
if (S.top == S.base)return ERROR;
e = *--S.top;
return OK;
}
int main()
{
Sqstatic stack;
Initstack(stack);
int i,n;
printf("请输入栈元素:\n");
int x;
for ( i = 0; i < 5; i++)
{
scanf_s("%d", &x);
Push(stack, x);
}
printf("输出栈元素:\n");
for (i = 0; i < 5; ++i)
{
if(Pop(stack ,n))
printf("%d ", n);
}
return 0;
}