




已阅读5页,还剩8页未读, 继续免费阅读
版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
江南大学物联网工程学院上机报告课程名称 数据结构 上机名称 实验2 上机日期 2013- 1-31 班 级 计科1203 姓 名 汪俊 学号 1030412314上机报告要求 1上机名称 2上机要求 3上机环境 4程序清单(写明运行结果) 5上机体会 1. 上机名称 实验内容一、二2. 上机要求 一、1.栈,2.队列二、3.算术表达式3. 上机环境 Visual C+ 6.04.程序清单(写明运行结果) 一、1.#include#define Maxsize 100typedef int Elemtype;typedef structElemtype dataMaxsize;int top;Seqstack;void Initstack(Seqstack &s)s.top=-1;int Stackempty(Seqstack &s)if(s.top=-1)return 1;elsereturn 0;int Stackfull(Seqstack &s)if(s.top=Maxsize-1)return 1;elsereturn 0;void Push(Seqstack &s,int x)if(Stackfull(s)printf(栈满n);elses.top+;s.datas.top=x;void Display(Seqstack s)if(s.top=-1)printf(n栈空);elsewhile(s.top!=-1)printf(%d ,s.datas.top);s.top-;Elemtype Pop(Seqstack &s)if(Stackempty(s)return 0;elsereturn s.datas.top-;Elemtype Stacktop(Seqstack s)if(Stackempty(s)return 0;elsereturn s.datas.top;void main()int n,i,k,h,x1,x2,select=1;Seqstack S;printf(创建一个空栈!n);Initstack(S);printf(输入栈里的元素个数n);scanf(%d,&n);for(i=0;in;i+)printf(输入入栈元素值n);scanf(%d,&k);Push(S,k);while(select)printf(n);printf(select 1:遍历顺序栈n);printf(select 2:入栈n);printf(select 3:出栈n);printf(select 4:取栈顶元素n);printf(select 0:退出n);printf(:请选择(1-4):n);scanf(%d,&select);switch(select)case 1:Display(S);break;case 2:printf(输入入栈元素值:n);scanf(%d,&h);Push(S,h);break;case 3:x1=Pop(S);printf(出栈元素=%dn,x1);break;case 4:x2=Stacktop(S);printf(栈顶元素=%dn,x2);break;运行结果2.#include#includetypedef struct nodeint data;struct node *next;QNode;typedef structQNode *front,*rear;LQueue;LQueue *Init_LQueue()/创建一个带头节点的空队LQueue *q;QNode*p;q=(LQueue *)malloc(sizeof(LQueue);/动态申请头尾指针节点p=(QNode *)malloc(sizeof(QNode);/申请链队头节点p-next=NULL;q-front=q-rear=p;return q;void In_LQueue(LQueue *q,int x)/入队QNode *p;p=(QNode *)malloc(sizeof(QNode);/申请新节点p-data=x;p-next=NULL;q-rear-next=p;/将申请的p节点连到队尾上q-rear=p;/尾指针后移一位int Empty_LQueue(LQueue *q)if (q-front=q-rear)return 1;elsereturn 0;/判断队列是否为空int Out_LQueue(LQueue *q,int &b )QNode *p;/int b=1;if (Empty_LQueue(q)printf(队空!);return 0;elsep=q-front-next;q-front-next=p-next;b=p-data;/x=p-data;free(p);if (q-front-next=NULL)q-rear=q-front;return b;void Display(LQueue *q)if(Empty_LQueue(q)printf(遍历为空!n);elseQNode *p=q-front-next;while(p!=NULL)printf(%d ,p-data);p=p-next;void main()printf(注意:新建的队列为空!n);LQueue *r;r=Init_LQueue();int x=0 ,select=1;while (select)printf(n);printf(selectm 1:遍历链队列n);printf(selectm 2:入队n);printf(selectm 3:出队n);printf(selectm 4:判断队是否为空n);printf(selectm 0:退出n);printf(请选择(1-4):n);scanf(%d,&select);if(select!=1 & select!=2 & select!=3 & select!=4 & select!=0)printf(您输入的数字不合法,请重新输入!n);switch(select)case 1:Display(r);break;case 2:printf(输入入队元素值:n);scanf(%d,&x);In_LQueue(r,x);break;case 3:x=Out_LQueue(r,x);if(x=0)printf(无法出队!n,x);elseprintf(出栈元素=%dn,x);break;case 4:if (Empty_LQueue(r)printf(队空!n);elseprintf(队不空!n);break;运行结果3.进制的转化#include# define Maxsize 100typedef int Elemtype;typedef structElemtype dataMaxsize;int top;Seqstack;void Initstack(Seqstack &s)s.top =0; int Stackempty(Seqstack s)if(s.top =0)return 1;elsereturn 0;int Stackfull(Seqstack s)if(s.top=Maxsize-1)return 1;elsereturn 0;void Push(Seqstack &s,int x)if(Stackfull(s)printf(栈满n);return ;elses.datas.top=x;s.top+;Elemtype Pop(Seqstack &s)Elemtype y;if(Stackempty(s)printf(栈空n);return 0;elses.top=s.top-1;y=s.datas.top;return y;Elemtype Stacktop(Seqstack &s)if(Stackempty(s)return 0;else return s.datas.top;void Dec_to_Ocx(int N)Seqstack S;Elemtype x;Initstack(S);int n=N;if(N0)printf(n出错,必须是正数。);return;if(!N)Push(S,0);while(N)Push(S,N%8);N=N/8;printf(十进制%d 转化为八进制 :,n);while(!Stackempty(S)x=Pop(S);printf(%d,x);printf(n);void main()int n,i,k,h,x1,x2,select=1;Seqstack S;printf(创建一个空栈!n);Initstack(S);printf(输入元素:n);scanf(%d,&n);Dec_to_Ocx(n);运行结果算术表达式#include#define MAXSIZE 20typedef struct char dateMAXSIZE;int top;Seqstack;int isop(char op)switch(op)case+:case-:case*:case/:return 1;default:return 0;int pri(char op)switch(op)case#:return -1;case(: return 0;case+:case-: return 1;case*:case/: return 2;default:return -1;void Postfi(Seqstack Ss,char st1 ,char st2 )int i=0,j=0;Ss.top+;Ss.dateSs.top=#;while(st1i!=#)if(st1i=0&st1i=pri(st1i)st2j+=Ss.dateSs.top-;Ss.top+;Ss.dateSs.top=st1i;i+;while(Ss.dateSs.top!=#)st2j+=Ss.dateSs.top-;st2j=0;void Initstack(Seqstack &Ss)Ss.top=-1;void main()char st120,st220;
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 公司礼仪提升活动方案
- 公司端午节文体活动方案
- 公司文汇活动方案
- 公司留深过年活动方案
- 公司活动设计策划方案
- 公司组织公益活动方案
- 公司组织建设活动方案
- 公司百人活动策划方案
- 公司搞运动会活动方案
- 公司福利娱乐活动方案
- 砂石销售提成管理制度
- 2025年湖南省中考生物试卷及答案
- 高效化学灭菌技术-洞察及研究
- 融媒体保密管理制度
- 2025至2030中国消防产业市场深度调研及发展前景及有效策略与实施路径评估报告
- 2025江苏扬州宝应县“乡村振兴青年人才”招聘67人笔试参考题库附答案详解
- 2025年高考全国二卷数学高考真题解析 含参考答案
- 动火安全作业票填写模板2022年更新
- 桥梁荷载试验
- 综合布线报价清单范本
- 矿山行业生产制造执行系统(MES)
评论
0/150
提交评论