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

加入VIP,免费下载
 

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

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

下载须知

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

版权提示 | 免责声明

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

c数据结构实验报告.docx

1、c 数据结构实验报告c+数据结构实验报告算法与数据结构;实验报告;实验一:栈与队列;一、实验目的;1、掌握栈和队列特点、逻辑结构和存储结构;2、熟悉对栈和队列的一些基本操作和具体的函数定义;3、利用栈和队列的基本操作完成一定功能的程序;二、实验任务;1.出顺序栈的类定义和函数实现,利用栈的基本操作;的转换;2.给出顺序队列的类定义和函数实现,并利用队列计;容;3.给出链栈的类定义和函数实现,并设计 算法与数据结构 实验报告 实验一:栈与队列 一、实验目的 1、掌握栈和队列特点、逻辑结构和存储结构 2、熟悉对栈和队列的一些基本操作和具体的函数定义。 3、利用栈和队列的基本操作完成一定功能的程序。

2、 二、实验任务 1. 出顺序栈的类定义和函数实现,利用栈的基本操作完成十进制数N与其它d进制数 的转换。(如N=1357,d=8) 2. 给出顺序队列的类定义和函数实现,并利用队列计算并打印杨辉三角的前n行的内 容。(n=8) 3. 给出链栈的类定义和函数实现,并设计程序完成如下功能:读入一个有限大小的整 数n,并读入n个数,然后按照与输入次序相反的次序输出各元素的值。 三、实验原理 1、将十进制数N转化为d进制时,用除去余数法,用d除N所得余数作为d进制当前 个位,将相除所得的商的整数部分作为新的N值重复上述计算,直到N为0为止。 将 前所得到的各余数反过来连接便得到最终结果。将每次求出的余

3、数入栈,求解结束后, 再依次出栈。 2、在杨辉三角中可用上一行的数来求出对应位置的下一行的内容。用队列保存上行内容, 每当由上行的两个数求出下行的一个数时,其中的前一个便需要删除,而求出的数就 入队。为便于求解,在每行的第一个位置添加一个0作为辅助。 3、输出操作应在读入所有输入的整数后才能进行,用栈来存储这些数据,调用入栈出栈 函数实现相关功能。 四、程序清单 第一题 #include #ifndef STACK_H #define STACK_H const int maxlen=256; typedef int elementtype; enum error_codesuccess, u

4、nderflow, overflow; class stack public: stack(); bool empty() const; bool full() const; error_code get_top(elementtype &x) const; error_code push(const elementtype x); error_code pop(); private: int count; elementtype data; ; stack:stack()count=0; bool stack:empty() const if(count=0) return true; re

5、turn false; error_code stack:get_top(elementtype &x) const if(empty() return underflow; elsex=data; return success; error_code stack:push(const elementtype x) if(full() return overflow; data=x; count+; return success; error_code stack:pop() if(empty() return underflow; count-; return success; bool s

6、tack:full() const if(count=maxlen) return true; return false; #endif void Dec_to_Ocx(int N, int d) stack S; int Mod,x; while(N!=0) Mod=N%d; 第二题 #include const int maxlen=256; typedef int elementtype; enum error_codesuccess, underflow, overflow; class queue public: queue(); bool empty()const; bool fu

7、ll()const; error_code get_front(elementtype &x) const; error_code append(const elementtype x); error_code serve(); private: int count; int front,rear; elementtype data; ; queue:queue()count=0; front=rear=0; bool queue:empty()const if(count=0) return true; return false; bool queue:full()const if(coun

8、t=maxlen-1) return true; return false; (Mod); N=N/d; while(!() _top(x); (); coutN; cind; Dec_to_Ocx(N,d); error_code queue:get_front(elementtype &x)const if(empty() return underflow; x=data; return success; error_code queue:append(const elementtype x) if(full() return overflow; rear=(rear+1)%maxlen;

9、 data=x; count +; return success; error_code queue:serve() if(empty() return underflow; front=(front+1)%maxlen; count-; return success; void Out_Number(int n) int s1,s2; queue Q; int i,j; error_code Ec; coutfor(i=2; in; s1=s2; Out_Number(n); 第三题 #include #ifndef STACK_H #define STACK_H const int max

10、len=256; typedef struct linklist int data; struct linklist *next; node; typedef int elementtype; enum error_codesuccess, underflow, overflow; class stack public: stack(); stack(); bool empty() const; bool full() const; error_code get_top(elementtype &x)const; error_code push(const elementtype x); er

11、ror_code pop(); private: int count; node*top; ; stack:stack() count = 0; top = NULL; bool stack:empty( )const return count = 0; bool stack:full( )const return false; error_code stack:get_top(elementtype &x)const if ( empty() ) return underflow; x = top - data; return success; stack:stack( ) while (

12、!empty() ) pop( ); error_code stack:push(const elementtype x )node* s; s = new node; s - data = x; s-next=top; top = s; count +; return success; error_code stack:pop( ) if ( empty() ) return underflow;node* u; u = top; top=top-next; u; count -; return success; #endif void read_write() stack S; int x

13、,n,i; coutn; for(i=1;ix; (x); while()!=true) _top(x); (); coutvoid main() read_write(); 五、运行结果 1、 2、 3、 实验二:单链表 一、实验目的: 1、理解线性表的链式存储结构。 2、熟练掌握动态链表结构及有关算法的设计。 3、根据具体问题的需要,设计出合理的表示数据的链表结构,并设计相关算法。 二、实验任务: 1、在一个递增有序的链表L中插入一个值为x的元素,并保持其递增有序特性。 实验数据:链表元素为(10,20,30,40,50,60,70,80,90,100),x分别为25,85,110和8。

14、2、 将单链表L中的奇数项和偶数项结点分解开,并分别连成一个带头结点的单链表,然后 再将这两个新链表同时输出在屏幕上,并保留原链表的显示结果,以便对照求解结果。 实验测试数据基本要求: 第一组数据:链表元素为(1,2,3,4,5,6,;3.求两个递增有序链表L1和L2中的公共元素,并;第一个链表元素为(1,3,6,10,15,16,;第二个链表元素为(1,2,3,4,5,6,7,8;第一个链表元素为(1,3,6,10,15,16,;三、实验原理;1、给定了递增的插入条件,需要搜索满足条件的插入;2、从链表A第一项开始,第一项插入链表B,第二 第一组数据:链表元素为 (1,2,3,4,5,6,7

15、,8,9,10,20,30,40,50,60) 第二组数据:链表元素为 (10,20,30,40,50,60,70,80,90,100) 3.求两个递增有序链表L1和L2中的公共元素,并以同样方式连接成链表L3。 实验测试数据基本要求: 第一组数据: 第一个链表元素为 (1,3,6,10,15,16,17,18,19,20) 第二个链表元素为 (1,2,3,4,5,6,7,8,9,10,18,20,30) 第二组数据: 第一个链表元素为 (1,3,6,10,15,16,17,18,19,20) 第二个链表元素为 (2,4,5,7,8,9,12,22) 三、实验原理 1、给定了递增的插入条件,需

16、要搜索满足条件的插入位置的前驱结点。从原表的第一个 元素开始,直到某一元素A的值大于要插入元素的值为止,A的前驱结点便是插入 位置。 2、从链表A第一项开始,第一项插入链表B,第二项插入链表C,依次类推,奇数项 插入链表B,偶数项插入链表C,直到无元素为止。 3、从AB表的第一项开始,若A第一项大于B第一项,B向后搜索,如果找到共同元 就将其插入表C,如无共同元素,表A从第二项搜索,依次类推。若A的第一项小 于B的第一项,则该元素没有公共项,A从第二项搜索,依次类推。若AB第一项相 同,则该数就是公共元素,插入表C,AB都从第二项搜索,依次类推。 四、程序清单 第一、二题 #include s

17、truct node int data; node *next; ; class list public: list(); list(); int length(); void input_elem(const int n); void print(); node *get_head(); void set_(node *L,int data); void jiou_list(node *P,node *Q,node *R); private: int count; node *head; ; list:list() head=new node; head-next=NULL; count=0

18、; list:list() node *p,*q; p = head-next; while(p!=NULL) q = p; p = p-next; q; int list:length() int n=0; node *p=head-next; while(p!=NULL) n+; p=p-next; return n; void list:input_elem(const int n) node *p,*pre; pre = head; for(int i=0;i p=new node; cinp-data; pre-next=p; pre=p; count+; pre-next=NULL

19、; void list:print() node *p; p=head; p=p-next; while (p!=NULL) cout coutcoutm; coutcase 1: coutcoutn; _elem(n); coutdata; _(_head(),data); (); cout第三题 #include #include typedef int elementtype; struct Node elementtype data; Node*next; ; class list private: Node*head; Node*real; public: coutcase 2: c

20、outcoutn; _elem (n); _list (_head(),_head(),_head(); coutcoutcoutcoutlist(); void common(list &L1,list &L2); void create(); friend ostream& operatorlist:list() head=new Node; real=head; ; void list:create() Node*u; elementtype x; cinx; while(x!=-1) u=new Node; u-data=x; real-next=u; real=u; cinx; re

21、al-next=NULL; ostream& operatornext; while(p!=NULL) outnext; outvoid list:common(list &L1,list &L2) Node*pa,*pb,*u; pa=next; pb=next; while(pa!=NULL&pb!=NULL) if(pa-datadata)pa=pa-next; else if(pa-datapb-data)pb=pb-next; else u=new Node; u-data=pa-data; real-next=u; real=u; pa=pa-next; pb=pb-next; r

22、eal-next=NULL; void main() list L1,L2,L3; int i;elementtype x; cout五、运行结果 1、 2、 3、 实验三:二叉树 一、实验目的: 1. 掌握二叉树的动态链表存储结构及表示。 2. 掌握二叉树的三种遍历算法(递归和非递归两类)。 3. 运用二叉树三种遍历的方法求解有关问题。 二、实验任务: 1. 建立一棵采用二叉链表结构存储的二叉树。 2. 分别采用递归和非递归两种方式对该二叉树进行先序、中序和后序遍历。 3.求二叉树的高度以及二叉树中叶子结点的数目;三、实验原理;1、按先序遍历的方法建立二叉树,结点的空孩子用#;非递归中序遍历

23、:遇到一个结点,就把它推入栈中,并;非递归后序遍历:遇到一个结点,把它推入栈中,遍历;3、求高度时,若T为空,则其高度为0,结束,否则;四、程序清单;递归并求叶子节点数和深度;#include 3. 求二叉树的高度以及二叉树中叶子结点的数目。 三、实验原理 1、按先序遍历的方法建立二叉树,结点的空孩子用#代替输入,先建立根结点在建立左 右子树,依次类推。 2、对二叉树的遍历是在对各子树分别遍历的基础上进行的,借助对整个二叉树的遍历算 法实现对左右子树的遍历。在先序遍历以T为根的二叉树时,访问根结点后,分别 对根结点*T的左右孩子为根的子树进行遍历,中序后序递归遍历也采用此思想。 非递归前序遍历

24、:遇到一个结点,就访问该结点,并把此结点入栈,然后下降去遍历 它的左子树。遍历完它的左子树后,从栈顶托出这个结点,并按照它的右链接指示的 地址再去遍历该结点的右子树结构。 非递归中序遍历:遇到一个结点,就把它推入栈中,并去遍历它的左子树。遍历完左 子树后,从栈顶托出这个结点并访问之,然后按照它的右链接指示的地址再去遍历该 结点的右子树。 非递归后序遍历:遇到一个结点,把它推入栈中,遍历它的左子树。遍历结束后,还 不能马上访问处于栈顶的该结点,而是要再按照它的右链接结构指示的地址去遍历该 结点的右子树。遍历遍右子树后才能从栈顶托出该结点并访问之。另外,需要给栈中 的每个元素加上一个特征位,以便当

25、从栈顶托出一个结点时区别是从栈顶元素左边回 来的(则要继续遍历右子树),还是从右边回来的(该结点的左、右子树均已周游)。特 征为left表示已进入该结点的左子树,将从左边回来;特征为right表示已进入该结 点的右子树,将从右边回来。 3、求高度时,若T为空,则其高度为0,结束,否则,T不为空时,可采用递归的方式, T的左右子树的高度分别能求出来,则二叉树的高度是其左右子树高度的最大值加1. 四、程序清单 递归并求叶子节点数和深度 #include typedef struct BiTNode char data; int bit; struct BiTNode *lchild,*rchild

26、,*parent; BiTNode; void InitBT(BiTNode *&t) t=NULL; int EmptyBT(BiTNode *t) if(t=0) return 1; else return 0; BiTNode *creatBT(BiTNode *t,int b) BiTNode *p; char ch; cinch; if(ch=#)return 0; else p=new BiTNode; p-data=ch; p-parent=t; p-bit=b; t=p; t-lchild=creatBT(t,0); t-rchild=creatBT(t,1); return

27、t; void preorder(BiTNode *t) if(!EmptyBT(t) coutlchild); preorder(t-rchild); void inorder(BiTNode *t)/中序遍历 if(!EmptyBT(t) inorder(t-lchild); coutrchild); void postorder(BiTNode *t)/后序遍历 if(!EmptyBT(t) postorder(t-lchild); postorder(t-rchild); cout void coutBT(BiTNode *t,int &m)/计算二叉树中叶子结点 if(!EmptyB

28、T(t) if(t-lchild=0) & (t-rchild=0) m+;/叶子结点 非递归遍历 #include coutBT(t-lchild,m); coutBT(t-rchild,m); int BTdepth(BiTNode *t)/求二叉树的深度 int i,j; if(EmptyBT(t) return 0; else i=BTdepth(t-lchild); j=BTdepth(t-rchild); return (ij?i:j)+1; void main() BiTNode *t; int m,n,i,d,q,k; char x; InitBT(t); coutcoutcoutcoutcoutBT(t,m); coutd=BTdepth(t); coutconst int MaxSize = 256; template struct BiNode T data; BiNode *lchild,*rchild; BiNode *btr; int

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

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