栈的实现很简单
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 | #include <stdio.h> #define MAXLEN 10 typedef struct { int data[MAXLEN]; int top; } SeqStack; int Push(SeqStack *S, int x) { if(S->top==MAXLEN-1) { printf("栈满!"); return 0; } else { S->top++; S->data[S->top]=x; return 1; } } int Pop(SeqStack *S, int *x) { if(S->top == -1) { printf("栈空!"); return 0; } else { *x=S->data[S->top]; S->top--; return 1; } } void InitStack( SeqStack *S ) { S->top=-1; /*初始化的顺序栈为空*/ } main() { SeqStack s; int x; int i; InitStack(&s); Push(&s,1); Push(&s,2); Push(&s,3); for(i = 1; i<4; i++) { Pop(&s,&x); printf("%d\n",x); } } |