栈是限定仅在表尾进行插入(push)和删除(pop)操作的线性表
Last in First Out LIFO 线性表的特例
#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
#define MAX_SIZE 5
#define ERROR 0
#define OK 1
typedef struct Node
{
int data[MAX_SIZE];
int top;
}SqStack;
int push(SqStack *stack, int value)
{
assert(stack);
if (stack->top >= MAX_SIZE - 1)
{
printf("stack full\n");
return ERROR;
}
stack->top++;
stack->data[stack->top] = value;
return OK;
}
int pop(SqStack *stack, int *value)
{
assert(stack);
if (stack->top < 0)
{
printf("stack empty\n");
return ERROR;
}
*value = stack->data[stack->top];
stack->top--;
return OK;
}
void dump(SqStack *stack)
{
for (int i=0;i<=stack->top;i++)
{
printf("%d ", stack->data[i]);
}
printf("\n");
}
int main()
{
SqStack *stack = (SqStack *)malloc(sizeof(SqStack));
stack->top = -1;
int ret = push(stack, 1);
ret = push(stack, 2);
ret = push(stack, 3);
ret = push(stack, 4);
ret = push(stack, 5);
ret = push(stack, 6);
dump(stack);
pop(stack, &ret);
pop(stack, &ret);
pop(stack, &ret);
dump(stack);
pop(stack, &ret);
pop(stack, &ret);
pop(stack, &ret);
dump(stack);
return 0;
}