1. 顺序栈的实现 #include <stdio.h> #include <stdlib.h> #define STACK_SIZE 1024 struct stack { int base[STACK_SIZE]; int top; }; //初始化栈 struct stack * init_stack() { struct stack * s = NULL; s = (struct stack *)malloc(sizeof(struct stack)); if (s == NULL) { printf("malloc error/n"); return NULL; } s->top = -1; return s; } //压栈:top 指向上一个free空间,将数据压入 int push_stack(struct stack * s, int num) { if (s->top > STACK_SIZE) { printf("push error/n"); return -1; } s->top ++; s->base[s->top] = num; return 0; } //出栈:将数据送出,top向下移一个空间 int pop_stack(struct stack *s) { int num; if (s->top < 0) { printf("pop error/n"); return -1; } num = s->base[s->top]; s->top--; return (num); } int main(int argc, char* argv[]) { struct stack * s; int num = 0; s = init_stack(); push_stack(s,123); num = pop_stack(s); printf("%d/n",num); }