ImageVerifierCode 换一换
格式:DOCX , 页数:62 ,大小:776.85KB ,
资源ID:27965308      下载积分:3 金币
快捷下载
登录下载
邮箱/手机:
温馨提示:
快捷下载时,用户名和密码都是您填写的邮箱或者手机号,方便查询和重复下载(系统自动生成)。 如填写123,账号就是123,密码也是123。
特别说明:
请自助下载,系统不会自动发送文件的哦; 如果您已付费,想二次下载,请登录后访问:我的下载记录
支付方式: 支付宝    微信支付   
验证码:   换一换

加入VIP,免费下载
 

温馨提示:由于个人手机设置不同,如果发现不能下载,请复制以下地址【https://www.bdocx.com/down/27965308.html】到电脑端继续下载(重复下载不扣费)。

已注册用户请登录:
账号:
密码:
验证码:   换一换
  忘记密码?
三方登录: 微信登录   QQ登录  

下载须知

1: 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。
2: 试题试卷类文档,如果标题没有明确说明有答案则都视为没有答案,请知晓。
3: 文件的所有权益归上传用户所有。
4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
5. 本站仅提供交流平台,并不能对任何下载内容负责。
6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

版权提示 | 免责声明

本文(算法与数据结构实验报告.docx)为本站会员(b****5)主动上传,冰豆网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对上载内容本身不做任何修改或编辑。 若此文所含内容侵犯了您的版权或隐私,请立即通知冰豆网(发送邮件至service@bdocx.com或直接QQ联系客服),我们立即给予删除!

算法与数据结构实验报告.docx

1、算法与数据结构实验报告算法与数据结构实验报告算法与数据结构实验报告学院:计算机与信息学院专业班级:姓名:学号:实验一栈和队列实验目的:掌握栈和队列特点、逻辑结构和存储结构熟悉对栈和队列的一些基本操作和具体的函数定义。利用栈和队列的基本操作完成一定功能的程序。实验任务:1.给出顺序栈的类定义和函数实现,利用栈的基本操作完成十进制数N与其它d进制数的转换。(如N=1357,d=8)实验原理:将十进制数N转换为八进制时,采用的是“除取余数法”,即每次用8除N所得的余数作为八进制数的当前个位,将相除所得的商的整数部分作为新的N值重复上述计算,直到N为0为止。此时,将前面所得到的各余数反过来连接便得到最

2、后的转换结果。程序清单:#include#includeusingnamespacestd;typedefintDATA_TYPE;constintMAXLEN=100;enumerror_codesuccess,overflow,underflow;classstackpublic:stack();boolempty()const;error_codeget_top(DATA_TYPE&x)const;error_codepush(constDATA_TYPEx);error_codepop();boolfull()const;private:DATA_TYPEdataMAXLEN;intc

3、ount;stack:stack()count=0;boolstack:empty()constreturncount=0;error_codestack:get_top(DATA_TYPE&x)constif(empty()returnunderflow;elsex=datacount-1;returnsuccess;error_codestack:push(constDATA_TYPEx)if(full()returnoverflow;elsedatacount=x;count+;error_codestack:pop()if(empty()returnunderflow;elsecoun

4、t-;returnsuccess;boolstack:full()constreturncount=MAXLEN;voidmain()stackS;intN,d;cout请输入一个十进制数N和所需转换的进制dNd;if(N=0)cout输出转换结果:Nendl;while(N)S.push(N%d);N=N/d;cout输出转换结果:endl;while(!S.empty()S.get_top(N);coutN;S.pop();coutendl;while(!S.empty()S.get_top(x);coutx;S.pop();测试数据:N=1348d=8运行结果:2.给出顺序队列的类定义和

5、函数实现,并利用队列计算并打印杨辉三角的前n行的内容。(n=8)实验原理:杨辉三角的规律是每行的第一和最后一个数是1,从第三行开始的其余的数是上一行对应位置的左右两个数之和。因此,可用上一行的数来求出对应位置的下一行内容。为此,需要用队列来保存上一行的内容。每当由上一行的两个数求出下一行的一个数时,其中的前一个便需要删除,而新求出的数就要入队。程序清单:#include#includeusingnamespacestd;typedefintDATA_TYPE;constintMAXLEN=100;enumerror_codesuccess,underflow,overflow;classque

6、uepublic:queue();boolempty()const;error_codeget_front(DATA_TYPE&x)const;error_code append(const DATA_TYPE x);error_codeserve();boolfull()const;private:intfront,rear;DATA_TYPEdataMAXLEN;queue:queue()rear=0;front=0;boolqueue:empty()constreturn(front%MAXLEN=rear%MAXLEN);error_codequeue:get_front(DATA_T

7、YPE&x)constif(empty()returnunderflow;elsex=datafront%MAXLEN;returnsuccess;error_codequeue:append(constDATA_TYPEx)if(full()returnoverflow;elsedatarear%MAXLEN=x;rear+;error_codequeue:serve()if(empty()returnunderflow;elsefront+;returnsuccess;boolqueue:full()constreturn(rear+1)%MAXLEN=front);voidmain()q

8、ueueQ;intnum1,num2;inti=0;cout1endl;Q.append(1);num1=0;num2=1;for(i=0;i=7;i+)intj=0;intk=0;num1=0;for(j=0;j=i;j+)Q.get_front(num2);Q.serve();coutnum1+num2;Q.append(num1+num2);num1=num2;cout1endl;Q.append(1);运行结果:3.给出链栈的类定义和函数实现,并设计程序完成如下功能:读入一个有限大小的整数n,并读入n个数,然后按照与输入次序相反的次序输出各元素的值。实验原理:依次将栈中的元素出栈,因为

9、栈的一个特点就是先进后出,这样,当将原栈为空时,输出与输入次序相反,从而实现了本题的要求。程序清单:#include#includeusingnamespacestd;typedefintDATA_TYPE;typedefstructLNodeDATA_TYPEdata;LNode*next;LNode;enumerror_coderange_error,success,underflowclasslinkstackpublic:linkstack();linkstack();boolempty()const;error_codepush(constDATA_TYPEx);error_code

10、get_top(DATA_TYPE&x)const;error_codepop();private:LNode*top;intcount;DATA_TYPEdata;linkstack:linkstack()top=NULL;count=0;boollinkstack:empty()constreturn(count=0);error_codelinkstack:push(constDATA_TYPEx)LNode*s=newLNode;s-data=x;s-next=top;top=s;count+;returnsuccess;error_code&x)constlinkstack:get_

11、top(DATA_TYPEif(empty()returnunderflow;elsex=top-data;returnsuccess;error_codelinkstack:pop()if(empty()returnunderflow;elseLNode*u=newLNode;u=top;top=top-next;deleteu;count-;returnsuccess;linkstack:linkstack()while(!empty()pop();voidmain()linkstackL;intn;cout请任意输入一个整数n:n;for(inti=1;i=n;i+)L.push(i);

12、while(!L.empty()L.get_top(i);couti;L.pop();测试数据:n=9i=1运行结果:实验二单链表实验目的:理解线性表的链式存储结构。熟练掌握动态链表结构及有关算法的设计。根据具体问题的需要,设计出合理的表示数据的链表结构,并设计相关算法。实验任务:1.在一个递增有序的链表L中插入一个值为x的元素,并保持其递增有序特性。实 验 数 据 : 链 表 元 素 为(10,20,30,40,50,60,70,80,90,100),x分别为25,85,110和8。实验原理:给出了要插入的条件,但没有给定插入位置。因此,需要搜索满足这一条件的插入位置的前驱结点而不是序号。程

13、序清单:#includeusingnamespacestd;enumerror_codesuccess,overflow,underflow,rangeerror;typedefintelementtype;typedefstructLinkNodeelementtype data;structLinkNode *next;node;classlistprivate:int count;node *head;public:list();list();public:error_code get_element(const int i,elementtype&x)const;node*get_he

14、ad()constreturnhead;voidcreate();voiddisplay();voidinsert1(elementtypex);list:list()head=newnode;head-next=NULL;count=0;void list:create()elementtypex;node*s,*rear;cinx;rear=head;while(x!=-9999)count+;s=newnode;s-data=x;s-next=NULL;rear-next=s;rear=s;cinx;voidlist:display()node*p;p=head-next;while(p

15、!=NULL)coutdatanext;coutnext!=NULL&P-next-datanext;if(P-next=NULL|P-next-datax)u=newnode;u-data=x;u-next=P-next;P-next=u;count+;voidmain()listL;elementtypex;L.create();L.display();cout请输入要插入的值xx;L.insert(x);L.display();运行结果:X=25X=85X=110X=82.将单链表中的奇数项和偶数项结点分解开,并分别连成一个带头结点的单链表,然后再将这两个新链表同时输出在屏幕上,并保留原

16、链表的显示结果,以便对照求解结果。实验测试数据:第一组数据:链表为(1,2,3,4,5,6,7,8,9,10,20,30,40,50,60)第二组数据:链表为(10,20,30,40,50,60,70,80,90,100)实验原理:依据题目的要求,需要再创建两个新链表来存储分离后的奇偶项,而奇偶项可以根据数字来控制,把他们取出来并重新连起来。程序清单:#includeusingnamespacestd;enumerror_codesuccess,overflow,underflow,rangeerror;typedefintelementtype;typedefstructLinkNodeel

17、ementtype data;structLinkNode *next;node;classlistprivate:int count;node *head;public:list();list();public:node*get_head()constreturnhead;voidcreate();voiddisplay();list:list()head=newnode;head-next=NULL;count=0;void list:create()elementtypex;node*s,*rear;cinx;rear=head;while(x!=-9999)count+;s=newno

18、de;s-data=x;s-next=NULL;rear-next=s;rear=s;cinx;voidlist:display()node*p;p=head-next;while(p!=NULL)coutdatanext;coutnext;while(PA!=NULL)if(PA-data)%2!=0)s=newnode;s-data=PA-data;PB-next=s;PB=s;PB-next=NULL;PA=PA-next;elses=newnode;s-data=PA-data;PC-next=s;PC=s;PC-next=NULL;PA=PA-next;voidmain()listL

19、,L1,L2;L.create();divide(L,L1,L2);cout原链表:endl;L.display();cout偶链表:endl;L2.display();cout奇链表:next和B.head-next。在pa,pb均非空时,根据其值的大小关系可能有如下三种情况。(1).pa-data=pb-data:搜索到公共元素,应在C表表尾插入一个结点,其值为pa-data,然后继续A表中下一个元素的搜索,即pa=pa-next,同时pb也往后移。(2).pa-datapb-data:表明A表中这一元素可能在B表当前元素的后面,因此要往B表的后面搜索,故而执行pb=pb-next,然后继

20、续搜索。(3).pa-datadata:表明A中这一元素在B中不存在,因而执行pa=pa-next以继续对A表中下一个元素的判断。反复执行上述比较,直到pa,pb至少有一个为空为止。此时,剩余的非空部分没有所需要的公共元素,因而搜索结束。程序清单:#include#includeusingnamespacestd;typedefstructsnodeintdata;structsnode*next;node;enumerror_codearrange_error,success;classlistpublic:list();voidcreate2();intlength()const;erro

21、r_codeget_element(constinti,int&x)const;error_codeinsert(constint&x);error_codedelete_element(constinti);node*locate(constintx)const;node*get_head()returnhead;voidlist:gongyou(list&L1,list&L2)voidprint();private:intcount;node*head;list:list()head=newnode;head-next=NULL;count=0;voidlist:create2()intx

22、;node*p=head;node*s;coutx;while(x!=-1)s=newnode;s-data=x;s-next=NULL;p-next=s;p=s;coutx;intlist:length()constreturncount;error_codelist:get_element(constinti,int&x)constintj=1;node*p=head-next;while(p!=NULL&j!=i)p=p-next;j+;if(p=NULL)returnarrange_error;x=p-data;returnsuccess;voidlist:gongyou(list&L

23、1,list&L2)node *p1=L1.head-next; node*p2=L2.head-next;node*p3=head;node*u;while(p1!=NULL&p2!=NULL)if(p1-data=p2-data)u=newnode;u-data=p1-data;p3-next=u;p1=p1-next;p2=p2-next;p3=p3-next;elseif(p1-datadata)p1=p1-next;elsep2=p2-next;p3-next=NULL;node*list:locate(constintx)constnode*p=head-next;while(p!=NULL)if(p-data=x)returnp;p=p-next;returnNULL;voidlist:divide(list&B,list&C)node*u;node*pa=head-next;

copyright@ 2008-2022 冰豆网网站版权所有

经营许可证编号:鄂ICP备2022015515号-1