#include <stdio.h>
#include <stdlib.h>
#define OK 1
#define ERROR 0
#define TRUE 1
#define FALSE 0
typedef int State;
typedef int SElemType;
typedef struct StackNobe
{
SElemType data;
struct StackNobe *next;
}StackNobe, *LinkStackPtr;
typedef struct LinkStack
{
LinkStackPtr top;
int count;
}LinkStack;
State initStack(LinkStack *S);
State destroyStack(LinkStack *S);
State pushStack(LinkStack *S, int e);
State popStack(LinkStack *S, int *e);
State printStack(LinkStack S);
State initStack(LinkStack *S)
{
S->top = NULL;
S->count = 0;
return OK;
}
State destroyStack(LinkStack *S)
{
LinkStackPtr p_free = NULL;
while (S->top != NULL)
{
p_free = S->top;
S->top = S->top->next;
free(p_free);
}
return OK;
}
State pushStack(LinkStack *S, int e)
{
LinkStackPtr p;
p = (LinkStackPtr)malloc(sizeof(StackNobe));
if (!p)
return ERROR;
p->data = e;
p->next = S->top;
S->top = p;
S->count++;
return OK;
}
State popStack(LinkStack *S, int *e)
{
if (!S->count)
return ERROR;
*e = S->top->data;
LinkStackPtr p_free = S->top;
S->top = S->top->next;
free(p_free);
return OK;
}
State printStack(LinkStack S)
{
while (S.top != NULL)
{
printf("%d ", S.top->data);
S.top = S.top->next;
}
return OK;
}
int main(void)
{
LinkStack stack;
int e;
initStack(&stack);
pushStack(&stack, 1);
pushStack(&stack, 2);
popStack(&stack, &e);
pushStack(&stack, 3);
pushStack(&stack, 4);
printf("弹栈了 %d\n", e);
printStack(stack);
destroyStack(&stack);
return 0;
}