下载此文档

数据结构书中源程序3.1-3.4.doc


文档分类:IT计算机 | 页数:约10页 举报非法文档有奖
1/10
下载提示
  • 1.该资料是网友上传的,本站提供全文预览,预览什么样,下载就什么样。
  • 2.下载该文档所得收入归上传者、原创者。
  • 3.下载的文档,不会出现我们的网址水印。
1/10 下载此文档
文档列表 文档介绍
#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转载请标明出处.

非法内容举报中心
文档信息
  • 页数10
  • 收藏数0 收藏
  • 顶次数0
  • 上传人274030239
  • 文件大小59 KB
  • 时间2020-08-21