参考自:http://blog.csdn.net/u010187139/article/details/46673163
源码下载:http://pan.baidu.com/s/1bnFIEGv
// // main.c // LineStackDemo // // Created by zhanggui on 15/8/12. // Copyright (c) 2015年 zhanggui. All rights reserved. // #include <stdio.h> #define MAXSIZE 20 #define ERROR 0 #define OK 1 typedef int Status; typedef int SelemType; //定义一个结构体类型 typedef struct { SelemType data[MAXSIZE]; int top; } SqStack; //入栈 Status push(SqStack *s,SelemType e) { //判断是否栈满 if (s->top==MAXSIZE-1) { return ERROR; } s->top++; s->data[s->top] = e; return OK; } //出栈 Status pop(SqStack *s,SelemType *e) { if (s->top==-1) { return ERROR; } *e = s->data[s->top]; s->top--; return OK; } //初始化 Status initStack(SqStack *s) { s->top=-1; return OK; } //输出栈中的所有元素 void stackTraverse(SqStack s) { if (s.top==-1) { printf("栈中无元素"); }else { while (s.top!=-1) { printf("%d ",s.data[s.top]); s.top--; } printf(" "); } } //创建 void createStack(SqStack *s) { if (s->top!=MAXSIZE-1) { int i; for(i=0;i<MAXSIZE-4;i++) { push(s, 4); } } } int main(int argc, const char * argv[]) { SqStack s; SelemType e; initStack(&s); createStack(&s); if (s.top!=-1) { pop(&s, &e); } stackTraverse(s); return 0; }