数据结构实验三之顺序栈.doc

上传人:罗晋 文档编号:5656325 上传时间:2020-07-20 格式:DOC 页数:5 大小:18.50KB
返回 下载 相关 举报
数据结构实验三之顺序栈.doc_第1页
第1页 / 共5页
数据结构实验三之顺序栈.doc_第2页
第2页 / 共5页
数据结构实验三之顺序栈.doc_第3页
第3页 / 共5页
数据结构实验三之顺序栈.doc_第4页
第4页 / 共5页
数据结构实验三之顺序栈.doc_第5页
第5页 / 共5页
亲,该文档总共5页,全部预览完了,如果喜欢就下载吧!
资源描述

《数据结构实验三之顺序栈.doc》由会员分享,可在线阅读,更多相关《数据结构实验三之顺序栈.doc(5页珍藏版)》请在三一文库上搜索。

1、#include#include#define MAXSIZE 100typedef int DataType;typedef struct stackDataType dataMAXSIZE;int top;sqstack;sqstack *InitStack(sqstack *S)/顺序栈的初始化S-top=-1;return S;void push(sqstack *S,DataType x)/顺序栈的元素入栈if(S-topMAXSIZE-1)printf(error!);elseS-top+;S-dataS-top=x;DataType pop(sqstack *S)/顺序栈的元素出

2、栈int x;if(S-top=-1)printf(Underflow!);elsex=S-dataS-top;printf(出栈的值为:%5dn,S-dataS-top);S-top-;return x;DataType GetTop(sqstack *S)/顺序栈取顶元素if(S-top=-1)printf(Underflow!n);return 0;elsereturn S-dataS-top;int Empty(sqstack *S)/顺序栈的判空if(S-top=-1)return 1;else return 0;void Digit_conversion(sqstack *S)in

3、t x,y,dataMAXSIZE,c,count=0;InitStack(S);printf(请输入一个十进制数,在输入一个其他进制数:n);scanf(%d%d,&x,&y);printf(将%d的十进制数将其转化为%d进制数:n,x,y);while(x)push(S,x%y);x=x/y;while(!Empty(S)datacount=pop(S);count +;c=count;printf(转换后的结果为:t);for(count=0;count9)printf(%c,datacount+A-10); else printf(%dt,datacount);printf(%n);v

4、oid menu()printf(* *n);printf(* 0 退 出 *n);printf(* 1 顺序栈的初始化 *n);printf(* 2 顺序栈的入栈 *n);printf(* 3 顺序栈的出栈 *n);printf(* 4 获取顺序栈的顶元素 *n);printf(* 5 顺序栈的的判空 *n);printf(* 6 利用顺序栈将十进制 *n);printf(* 转换为其他进制数 *n);printf(*n);int main()sqstack *L,*Sqstack;int flag=1,number1,number2,data,a;char ch,ch1;L=(sqstac

5、k *)malloc(sizeof(sqstack);menu();while(flag)doprintf(请输入一个07的数:.);scanf(%d,&number1);ch=getchar();if(number17)printf(输入有误,请重新输入!n);while(number17);switch(number1)case 0:flag=0;break;case 1:Sqstack=InitStack(L);break;case 2:printf(你确定要输入入栈的数据?如果要输入请输入y/Y:n);ch1=getchar();while(ch1=y|ch1=Y)printf(输入你

6、要入栈的数据:n);scanf(%d,&data);fflush(stdin);push(Sqstack,data);printf(你还要输入入栈的数据?如果要输入请输入y/Y:n);ch1=getchar();break;case 3:pop(Sqstack);break;case 4:number2=GetTop(Sqstack);printf(输入取出的栈顶元素:n);printf(其值为:%5dn,number2);break;case 5:a=Empty(Sqstack);if(a=1)printf(顺序栈为空!n);elseprintf(顺序栈不为空!n);break;case 6:Digit_conversion(Sqstack);break;return 0;

展开阅读全文
相关资源
猜你喜欢
相关搜索

当前位置:首页 > 科普知识


经营许可证编号:宁ICP备18001539号-1