#include<>#include<>#defineMAXSIZE10typedefstruct{ intdatas[MAXSIZE]; inttop;}SqStack;voidInitStack(SqStack*s){ s->top=-1;}//InitStackintEmptyStack(SqStack*s){//若栈空返回1;否则返回0。if(s->top==-1) return1; else return0;}//EmptyStackvoidPush(SqStack*s,inte){//若栈满返回0;否则将e入栈,并返回1。if(s->top==MAXSIZE-1) {printf("Overflow\n");} else {s->top++; s->datas[s->top]=e; }}//PushintPop(SqStack*s){// 若栈空则返回NULL;否则返回栈顶元素,栈顶指示器递减。inte;if(EmptyStack(s)) {printf("Underflow\n"); return(0);}else{e=s->datas[s->top]; s->top--; returne;}}//Popmain(){SqStack*s;inti,e;s=(SqStack*)malloc(sizeof(SqStack));InitStack(s);printf("请按题目要求输入5个整数:");for(i=0;i<5;i++){ scanf("%d",&e); Push(s,e);}printf("栈中所有元素出栈的序列为:");while(!(EmptyStack(s))){ e=Pop(s); printf("%4d",e);}printf("\n"); }/*程序运行结果:请按题目要求输入5个整数:136910栈中所有元素出栈的序列为:109631Pressanykeytocontinue*/#include<>#include<>typedefstructSNode{ intdata;//数据元素的类型为整型 structSNode*next;}SNode;SNode*InitStack(SNode*top){ top=NULL;return(top);}//InitStackintEmptyStack(SNode*top){//若栈空返回1;否则返回0。if(top==NULL) return1;else return0;}//EmptyStackSNode*PushStack(SNode*top,inte){//在链栈的栈顶插入一个值为e的结点SNode*t; t=(SNode*)malloc(sizeof(SNode)); t->data=e; t->next=top; top=t; returntop;}//PushStackSNode*PopStack(SNode*top){//将链栈的栈顶元素删除SNode*t; if(top==NULL)//空栈 {printf("Underflow\n");//下溢 return0; } else {t=top;top=top->next; free(t); returntop; }}//PopStackin
数据结构书中源程序3.1-3.4 来自淘豆网www.taodocs.com转载请标明出处.