数据结构实验二题目一栈和队列实验报告

上传人:仙*** 文档编号:88463187 上传时间:2022-05-11 格式:DOC 页数:10 大小:58KB
收藏 版权申诉 举报 下载
数据结构实验二题目一栈和队列实验报告_第1页
第1页 / 共10页
数据结构实验二题目一栈和队列实验报告_第2页
第2页 / 共10页
数据结构实验二题目一栈和队列实验报告_第3页
第3页 / 共10页
资源描述:

《数据结构实验二题目一栈和队列实验报告》由会员分享,可在线阅读,更多相关《数据结构实验二题目一栈和队列实验报告(10页珍藏版)》请在装配图网上搜索。

1、-数据构造实验报告实验名称: 实验2栈和队列学生:班级:班序号:*:日 期: 一、 实验要求1、实验目的: 进一步掌握指针、模板类、异常处理的使用掌握栈的操作的实现方法掌握队列的操作的实现方法学习使用栈解决实际问题的能力学习使用队列解决实际问题的能力 2、实验容:根据栈和队列的抽象数据类型的定义,按要现一个栈或一个队列。要求:实现一个共享栈实现一个链栈实现一个循环队列实现一个链队列编写测试main()函数测试线性表的正确性二、程序分析2.1 存储构造顺序栈、链栈和顺序队列顺序栈 链栈 顺序队列2.2 关键算法分析A、实现一个共享栈: a、伪代码实现入栈操作:如果栈满,则抛出上溢异常;判断是插在

2、栈1还是栈2,如果在栈1插入,则栈顶指针top1加1,在top1处填入*,如果在栈2插入,则栈顶指针top2加1,在top2处填入*。出栈操作:如果栈空,则抛出下溢异常; 判断是栈1出栈还是栈2,如果是栈1,则输出栈1栈顶元素,并且top1减1,如果是栈2,则输出栈2栈顶元素,并且top2加1。得到栈顶元素操作:如果栈空,则抛出下溢异常; 判断是栈1出栈还是栈2,如果是栈1,则输出栈1栈顶元素,如果是栈2,则输出栈2栈顶元素。b、算法实现:void shareseqstack:push(int *,int pushnumber)/进栈操作if(top1+1=top2)/异常处理throw 上溢

3、;if(pushnumber=1)/判断栈1还是栈2data+top1=*;if(pushnumber=2)data-top2=*;void shareseqstack:pop(int popnumber)/出栈操作if(popnumber=1) /异常处理 if(top1=-1) throw 下溢;else coutdatatop1-endl;if(popnumber=2) /异常处理if(top2=seqstack) throw 下溢;else coutdatatop2+endl;void shareseqstack:gettop(int getnumber)/得到栈顶元素if(getnu

4、mber=1) /判断栈1还是栈2if(top1=-1) throw 下溢; /异常处理coutdatatop1endl;if(getnumber=2)if(top2=seqstack) throw 下溢;coutdatatop2data=*;p-ne*t=top;top=p;void linkstack:pop()/出栈操作if(empty() throw 下溢;/异常处理int *=top-data;Node*p=new Node; /定义新结点p=top;top=top-ne*t;delete p;cout* ;void linkstack:gettop()/得到栈顶元素if(empty

5、() throw 下溢;/异常处理coutdatane*t;delete p;C、实现一个循环队列a、 伪代码实现:入队:判断假设为队满,抛出异常,尾指针后移,指向入队元素。出队:判断假设为队空,抛出异常,头指针后移,输出队头元素。b、 算法实现void circlequeue:enqueue(int *)/进队操作if(rear+1)%queuesize=front) throw 上溢;/异常处理rear=(rear+1)%queuesize;datarear=*;void circlequeue:dequeue()/出队操作if(rear=front) throw 下溢;/异常处理fron

6、t=(front+1)%queuesize;coutdatafrontendl;void circlequeue:getfront()得到队头元素if(rear=front) throw 下溢;/异常处理coutdata(front+1)%queuesizeendl;void circlequeue:getlength()/得到队列长度cout(rear-front+queuesize)%queuesize)ne*t=new Node;rear=rear-ne*t;rear-data=*;rear-ne*t=NULL;void linkqueue:dequeue()/出队操作Node*p=fr

7、ont-ne*t;front-ne*t=p-ne*t;int *=p-data;delete p;if(!(front-ne*t)rear=front;cout*ne*t) throw 上溢;/异常处理coutne*t-data)ne*t;delete front;front=rear;2.3 其他源代码#includeusing namespace std;struct Node /定义结点int data;struct Node*ne*t;const int seqstack=100;class shareseqstack /定义共享栈 public:shareseqstack();voi

8、d push(int *,int pushnumber);void pop(int popnumber);void gettop(int getnumber);private:int dataseqstack;int top1;int top2;shareseqstack:shareseqstack()/构造top1=-1;top2=seqstack;void shareseqstack:push(int *,int pushnumber)/进栈操作if(top1+1=top2)/异常处理throw 上溢;if(pushnumber=1)/判断栈1还是栈2data+top1=*;if(push

9、number=2)data-top2=*;void shareseqstack:pop(int popnumber)/出栈操作if(popnumber=1) /异常处理 if(top1=-1) throw 下溢;else coutdatatop1-endl;if(popnumber=2)/异常处理if(top2=seqstack) throw 下溢;else coutdatatop2+endl;void shareseqstack:gettop(int getnumber)/得到栈顶元素if(getnumber=1) /判断栈1还是栈2if(top1=-1) throw 下溢;/异常处理cou

10、tdatatop1endl;if(getnumber=2)if(top2=seqstack) throw 下溢;coutdatatop2ne*t=NULL;linkqueue();void enqueue(int *);void dequeue();void getfront();bool empty()return front=rear true:false;private:Node*front;Node*rear;void linkqueue:enqueue(int *)/进队操作rear-ne*t=new Node;rear=rear-ne*t;rear-data=*;rear-ne*t

11、=NULL;void linkqueue:dequeue()/出队操作Node*p=front-ne*t;front-ne*t=p-ne*t;int *=p-data;delete p;if(!(front-ne*t)rear=front;cout*ne*t) throw 上溢;/异常处理coutne*t-data)ne*t;delete front;front=rear;class linkstack/定义链栈public:linkstack()top=NULL;linkstack();void push(int *);void pop();void gettop();bool empty(

12、)return(NULL=top) true:false;private:struct Node*top;void linkstack:push(int *)/进栈操作Node*p=new Node;/定义新结点p-data=*;p-ne*t=top;top=p;void linkstack:pop()/出栈操作if(empty() throw 下溢;/异常处理int *=top-data;Node*p=new Node; /定义新结点p=top;top=top-ne*t;delete p;cout* ;void linkstack:gettop()/得到栈顶元素if(empty() thro

13、w 下溢;/异常处理coutdatane*t;delete p;const int queuesize=1000;/定义循环队列class circlequeuepublic:circlequeue()front=rear=0;void enqueue(int *);void dequeue();void getfront();void getlength();bool empty()return front=rear true:false;private:int dataqueuesize;int front;int rear;void circlequeue:enqueue(int *)/

14、进队操作if(rear+1)%queuesize=front) throw 上溢;/异常处理rear=(rear+1)%queuesize;datarear=*;void circlequeue:dequeue()/出队操作if(rear=front) throw 下溢;/异常处理front=(front+1)%queuesize;coutdatafrontendl;void circlequeue:getfront()得到队头元素if(rear=front) throw 下溢;/异常处理coutdata(front+1)%queuesizeendl;void circlequeue:getl

15、ength()/得到队列长度cout(rear-front+queuesize)%queuesize)endl;void main()/主函数 linkstack D;D.push(1);/进栈D.push(2);D.push(3);cout链栈的实现:endl出栈顺序为:;D.pop();/出栈D.pop();D.pop();D.push(1);D.push(2);D.push(3);coutendl;cout栈顶元素为:;D.gettop();D.linkstack();/析构coutendl;shareseqstack A;A.push(1,1);A.push(2,1);A.push(3

16、,1);A.push(10,2);A.push(9,2);A.push(8,2);cout共享栈的实现:endl标号为1的栈出栈元素为:;A.pop(1);cout标号为2的栈出栈元素为:;A.pop(2);cout标号为1的栈栈顶元素为:;A.gettop(1);cout标号为2的栈栈顶元素为:;A.gettop(2);coutendl;linkqueue C;cout链队列的实现:endl;C.enqueue(2);C.enqueue(4);C.enqueue(6);cout出队元素为:;C.dequeue();cout队头元素为:;C.getfront();C.linkqueue();c

17、outendl;circlequeue B;B.enqueue(11);B.enqueue(22);B.enqueue(33);B.enqueue(44);B.enqueue(55);cout循环队列的实现:endl;cout出队的元素为:;B.dequeue();cout队头元素为:;B.getfront();cout队列长度为:;B.getlength();3. 程序运行结果1、流程图2、测试结果4. 总结心得体会实践才能得出真知,在通过上机操作后,才发现了许多在平时上理论课没有想到的方方面面。编写程序时发现很多语法错误,很多英语单词记不熟,有的会记错,程序函数错用等等,以后需多多实践多多上机操作才能解决已有的问题下一步的改良:入栈可以手动输入,这样修改入栈元素就可以不必修改原程序了。. z

展开阅读全文
温馨提示:
1: 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
2: 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
3.本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
5. 装配图网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
关于我们 - 网站声明 - 网站地图 - 资源地图 - 友情链接 - 网站客服 - 联系我们

copyright@ 2023-2025  zhuangpeitu.com 装配图网版权所有   联系电话:18123376007

备案号:ICP2024067431-1 川公网安备51140202000466号


本站为文档C2C交易模式,即用户上传的文档直接被用户下载,本站只是中间服务平台,本站所有文档下载所得的收益归上传人(含作者)所有。装配图网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对上载内容本身不做任何修改或编辑。若文档所含内容侵犯了您的版权或隐私,请立即通知装配图网,我们立即给予删除!