//栈的顺序存储(与顺序表类似)
#include <stdio.h>
#include <stdlib.h>
#define N 32
//定义数据类型
typedef int datatype_t;
//定义结构体
typedef struct {
datatype_t data[N];
int top;
}seqstack_t;
//创建一个空的栈
seqstack_t *seqstack_create()
{
seqstack_t *s;
s = (seqstack_t *)malloc(sizeof(seqstack_t));
s->top = -1;
return s;
}
//判断栈是否为满
int seqstack_full(seqstack_t *s)
{
return s->top == N - 1 ? 1 : 0;
}
//判断栈是否为空
int seqstack_empty(seqstack_t *s)
{
return s->top == -1 ? 1 : 0;
}
//入栈(压栈)
int seqstack_push(seqstack_t *s, datatype_t value)
{
if(seqstack_full(s))
{
printf("seqstack is full\n");
return -1;
}
s->top++;
s->data[s->top] = value;
return 0;
}
//打印数据
int seqstack_show(seqstack_t *s)
{
int i = 0;
for(i = 0; i <= s->top; i++)
{
printf("%d ", s->data[i]);
}
putchar(10);
return 0;
}
//出栈(弹栈)
datatype_t seqstack_pop(seqstack_t *s)
{
datatype_t value;
if(seqstack_empty(s))
{
printf("seqstack is empty\n");
return -1;
}
value = s->data[s->top];
s->top--;
return value;
}
int main(int argc, const char *argv[])
{
seqstack_t *s = seqstack_create();
seqstack_push(s, 10);
seqstack_push(s, 20);
seqstack_push(s, 30);
seqstack_push(s, 40);
seqstack_push(s, 50);
seqstack_show(s);
printf("del = %d\n", seqstack_pop(s));
printf("del = %d\n", seqstack_pop(s));
printf("del = %d\n", seqstack_pop(s));
printf("del = %d\n", seqstack_pop(s));
printf("del = %d\n", seqstack_pop(s));
printf("del = %d\n", seqstack_pop(s));
return 0;
}