栈的顺序存储结构
#include <stdio.h>
#include <stdlib.h>
#define ERROR -1
typedef int ElemType;
typedef struct aStack* Stack;
struct aStack
{
ElemType *data;
int top;
int count;
};
Stack StackInit(int n);
int StackEmpty(Stack S);
int StackFull(Stack S);
ElemType StackTop(Stack S);
bool push(Stack st, ElemType e);
ElemType pop(Stack S);
int main()
{
int n, e;
scanf("%d", &n);
Stack S = StackInit(n);
for (int i = 0; i < n; i++)
{
scanf("%d", &e);;
push(S, e);
}
printf("%d\n", StackTop(S));
for (int i = 0; i < n - 1; i++)
{
printf("%d ", pop(S));
}
return 0;
}
Stack StackInit(int n)
{
Stack S = (Stack)malloc(sizeof(struct aStack));
S->data = (ElemType *)malloc(n * sizeof(ElemType));
S->count = n;
S->top = -1;
return S;
}
int StackEmpty(Stack S)
{
if (S->top == -1)
return ERROR;
return 1;
}
int StackFull(Stack S)
{
if (S->top == S->count - 1)
return ERROR;
return 1;
}
ElemType StackTop(Stack S)
{
if (S->top == -1)
return ERROR;
return S->data[S->top--];
}
bool push(Stack S, ElemType e)
{
if (StackFull(S) == -1)
return ERROR;
S->data[++S->top] = e;
return 1;
}
ElemType pop(Stack S)
{
if (StackEmpty(S) == -1)
return ERROR;
return S->data[S->top--];
}