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

加入VIP,免费下载
 

温馨提示:由于个人手机设置不同,如果发现不能下载,请复制以下地址【https://www.bdocx.com/down/8329834.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为止。此时,将前面所得到的各余数反过来连接便得到最后的转换结果。程序清单#include#includeusing nam

2、espace std;typedef int DATA_TYPE;const int MAXLEN=100;enum error_codesuccess,overflow,underflow;class stackpublic: stack(); bool empty()const; error_code get_top(DATA_TYPE &x)const; error_code push(const DATA_TYPE x); error_code pop(); bool full()const;private: DATA_TYPE dataMAXLEN; int count;stack:

3、stack()count=0;bool stack:empty()constreturn count=0;error_code stack:get_top(DATA_TYPE &x)constif(empty()return underflow;elsex=datacount-1;return success;error_code stack:push(const DATA_TYPE x)if(full()return overflow;elsedatacount=x;count+;error_code stack:pop()if(empty()return underflow;elsecou

4、nt-;return success;bool stack:full()constreturn count=MAXLEN;void main()stack S;int N,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=1348 d=8运行结果:2.给出

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

6、verflow;class queuepublic: queue(); bool empty()const; error_code get_front(DATA_TYPE &x)const; error_code append(const DATA_TYPE x); error_code serve(); bool full()const;private: int front,rear; DATA_TYPE dataMAXLEN;queue:queue()rear=0;front=0;bool queue:empty()const return (front%MAXLEN=rear%MAXLE

7、N);error_code queue:get_front(DATA_TYPE &x)constif(empty()return underflow;elsex=datafront%MAXLEN;return success;error_code queue:append(const DATA_TYPE x)if(full()return overflow;elsedatarear%MAXLEN=x;rear+;error_code queue:serve()if(empty()return underflow;elsefront+;return success;bool queue:full

8、()constreturn(rear+1)%MAXLEN=front);void main()queue Q;int num1,num2;int i=0;cout1endl;Q.append(1);num1=0;num2=1;for(i=0;i=7;i+)int j=0;int k=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.给出链栈的类定义和函数实现,并设计程序完成如下功能:读入一个有限

9、大小的整数n,并读入n个数,然后按照与输入次序相反的次序输出各元素的值。实验原理:依次将栈中的元素出栈,因为栈的一个特点就是先进后出,。这样,当将原栈为空时,输出与输入次序相反,从而实现了本题的要求。程序清单:#include#includeusing namespace std;typedef int DATA_TYPE;typedef struct LNodeDATA_TYPE data;LNode *next;LNode; enum error_coderange_error,success,underflow;class linkstackpublic: linkstack(); li

10、nkstack(); bool empty()const; error_code push(const DATA_TYPE x); error_code get_top(DATA_TYPE &x)const; error_code pop();private: LNode *top; int count; DATA_TYPE data;linkstack:linkstack()top=NULL;count=0;bool linkstack:empty()constreturn (count=0);error_code linkstack:push(const DATA_TYPE x)LNode

11、 *s=new LNode;s-data=x;s-next=top;top=s;count+;return success;error_code linkstack:get_top(DATA_TYPE &x)constif(empty()return underflow;elsex=top-data;return success;error_code linkstack:pop()if(empty()return underflow;elseLNode *u=new LNode;u=top;top=top-next;delete u;count-;return success;linkstac

12、k:linkstack()while(!empty()pop();void main()linkstack L;int n;cout请任意输入一个整数n:n;for(int i=1;i=n;i+)L.push(i);while(!L.empty()L.get_top(i);couti ;L.pop();测试数据:n=9 i=1运行结果:实验二:单链表实验目的:理解线性表的链式存储结构。熟练掌握动态链表结构及有关算法的设计。根据具体问题的需要,设计出合理的表示数据的链表结构,并设计相关算法。实验任务:在一个递增有序的链表L中插入一个值为x的元素,并保持其递增有序特性。1.实验数据:链表元素为(1

13、0,20,30,40,50,60,70,80,90,100),x分别为25,85,110和8。实验原理:给出了要插入的条件,但没有给定插入位置。因此,需要搜索满足这一条件的插入位置的前驱结点而不是序号。程序清单:#include#includeusing namespace std;typedef struct snode int data; struct snode *next;node;enum error_codearrange_error,success;class listpublic: list(); void create2(); int length() const; erro

14、r_code get_element(const int i,int &x) const; error_code insert(const int &x); error_code delete_element(const int i); node *locate(const int x) const; node *get_head()return head; void print();private: int count; node *head;list:list() head=new node; head-next=NULL; count=0;void list:create2() int

15、x; node *p=head; node *s; coutx; while(x!=-1) s=new node; s-data=x; s-next=NULL; p-next=s; p=s; coutx; int list:length() const return count;error_code list:get_element(const int i,int &x) const int j=1; node *p=head-next; while(p!=NULL&j!=i) p=p-next; j+; if(p=NULL) return arrange_error; x=p-data; r

16、eturn success;node *list:locate(const int x) const node *p=head-next; while(p!=NULL) if(p-data=x) return p; p=p-next; return NULL;error_code list:insert(const int &x) node *s; node *q=head; node *p=head-next; while(p!=NULL&p-datanext; if(p=NULL) s=new node; s-data=x; s-next=NULL; q-next=s; count+; e

17、lse s=new node; s-data=x; s-next=q-next; q-next=s; count+; return success;error_code list:delete_element(const int i) node *u; node *p=head; int j=0; while(j!=i-1&p!=NULL) p=p-next; j+; if(icount) return arrange_error; u=p-next; p-next=u-next; delete u; count-; return success;void list:print() node

18、*p=head-next; while(p!=NULL) coutdatanext; coutendl;void main() list l; int x; cout创建一个链表(输入-1结束):endl; l.create2(); cout输入要插入的数(输入-1结束):endl; coutx; while(x!=-1) l.insert(x); coutx; l.print();测试数据:链表元素为(10,20,30,40,50,60,70,80,90,100),x分别为25,85,110和8。运行结果:2.将单链表中的奇数项和偶数项结点分解开,并分别连成一个带头结点的单链表,然后再将这两

19、个新链表同时输出在屏幕上,并保留原链表的显示结果,以便对照求解结果。实验原理:依据题目的要求,需要再创建两个新链表来存储分离后的奇偶项,而奇偶项可以根据数字来控制,把他们取出来并重新连起来。程序清单:#include#includeusing namespace std;typedef struct snode int data; struct snode *next;node;enum error_codearrange_error,success;class listpublic: list(); void create2(); int length() const; error_code

20、 get_element(const int i,int &x) const; error_code insert(const int &x); error_code delete_element(const int i); node *locate(const int x) const; node *get_head()return head; divide(list &B,list &C); void print();private: int count; node *head;list:list() head=new node; head-next=NULL; count=0;void

21、list:create2() int x; node *p=head; node *s; coutx; while(x!=-1) s=new node; s-data=x; s-next=NULL; p-next=s; p=s; coutx; int list:length() const return count;error_code list:get_element(const int i,int &x) const int j=1; node *p=head-next; while(p!=NULL&j!=i) p=p-next; j+; if(p=NULL) return arrange

22、_error; x=p-data; return success;node *list:locate(const int x) const node *p=head-next; while(p!=NULL) if(p-data=x) return p; p=p-next; return NULL;void list:divide(list &B,list &C) node *u; node *pa=head-next; node *pb=B.get_head(); node *pc=C.get_head(); for(int i=0;pa!=NULL;i+,pa=pa-next) u=new

23、node; u-data=pa-data; if(i%2=0) pb-next=u; pb=pb-next; else pc-next=u; pc=pc-next; pb-next=NULL; pc-next=NULL; error_code list:insert(const int &x) node *s; node *q=head; node *p=head-next; while(p!=NULL&p-datanext; if(p=NULL) s=new node; s-data=x; s-next=NULL; q-next=s; count+; else s=new node; s-d

24、ata=x; s-next=q-next; q-next=s; count+; return success;error_code list:delete_element(const int i) node *u; node *p=head; int j=0; while(j!=i-1&p!=NULL) p=p-next; j+; if(icount) return arrange_error; u=p-next; p-next=u-next; delete u; count-; return success;void list:print() node *p=head-next; while

25、(p!=NULL) coutdatanext; coutendl;void main() list A,B,C; int x,y,z; A.create_R(); A.divide(B,C); cout原表:; A.output(); cout奇数表:; B.output(); coutnext和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,然后继续搜索。 (3). pa-datadata:表明A中这一元素在B中不存在,因而执行pa=pa-next以继续对A表中下一个元素的判断。 反复执行上述比较,直到pa,pb至少有一个为空为止。此时,剩余的非空部分没有所需要的公共元素,因而搜索结束。程序清单:#include

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

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