//顺序栈入栈,取栈顶元素,出栈
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#define MAXSIZE 100
typedef int SElemtype;
typedef struct
{
SElemtype *base;
SElemtype *top;
int stacksize;
} SqStack;
void InitStack(SqStack *S)
{
S->base = (SElemtype *)malloc(MAXSIZE * sizeof(SElemtype));
if (!S->base)
{
exit(0);
}
S->top = S->base; //top初始为base,空栈
S->stacksize = MAXSIZE;
}
void Push(SqStack *S, SElemtype e)
{
int i;
if (S->top - S->base == S->stacksize)
{
printf("err");
}
else
{
printf("输入");
scanf("%d", &e);
S->top++;
*S->top = e;
}
}
void Pop(SqStack *S, SElemtype e)
{
if (S->top == S->base)
{
printf("err");
}
else
{
e = *S->top;
printf("出栈%d ", e);
S->top--;
}
printf("\n");
}
SElemtype GetTop(SqStack *S)
{
int e;
if (S->top != S->base)
{
e = *S->top;
}
printf("栈顶元素是%d\n", e);
return 0;
}
int main()
{
SqStack S;
int e;
InitStack(&S);
Push(&S, e);
Push(&S, e);
Push(&S, e);
Push(&S, e);
GetTop(&S);
Pop(&S, e);
Pop(&S, e);
Pop(&S, e);
Pop(&S, e);
return 0;
}