数据结构(C语言版)实验报告.doc

上传人:暗伤 文档编号:4671085 上传时间:2021-10-25 格式:DOC 页数:26 大小:131.04KB
返回 下载 相关 举报
数据结构(C语言版)实验报告.doc_第1页
第1页 / 共26页
数据结构(C语言版)实验报告.doc_第2页
第2页 / 共26页
点击查看更多>>
资源描述

《数据结构(C语言版)实验报告.doc》由会员分享,可在线阅读,更多相关《数据结构(C语言版)实验报告.doc(26页珍藏版)》请在得力文库 - 分享文档赚钱的网站上搜索。

1、数据结构(C语言版) 实验报告学院 计算机科学与技术专业 计算机大类强化学号 xxx班级 xxx姓名 xxx指导教师 xxx实验1实验题目:单链表的插入和删除实验目的:了解和掌握线性表的逻辑结构和链式存储结构,掌握单链表的基本算法及相关的时间性能分析。实验要求:建立一个数据域定义为字符串的单链表,在链表中不允许有重复的字符串;根据输入的字符串,先找到相应的结点,后删除之。实验主要步骤:1、 分析、理解给出的示例程序。2、 调试程序,并设计输入数据(如:bat,cat,eat,fat,hat,jat,lat,mat,#),测试程序的如下功能:不允许重复字符串的插入;根据输入的字符串,找到相应的结

2、点并删除。3、 修改程序:(1) 增加插入结点的功能。(2) 将建立链表的方法改为头插入法。程序代码:#includestdio.h#includestring.h#includestdlib.h#includectype.htypedef struct node /定义结点char data10; /结点的数据域为字符串struct node *next; /结点的指针域ListNode;typedef ListNode * LinkList; / 自定义LinkList单链表类型LinkList CreatListR1(); /函数,用尾插入法建立带头结点的单链表LinkList Crea

3、tList(void); /函数,用头插入法建立带头结点的单链表ListNode *LocateNode(); /函数,按值查找结点void DeleteList(); /函数,删除指定值的结点void printlist(); /函数,打印链表中的所有值void DeleteAll(); /函数,删除所有结点,释放内存ListNode * AddNode(); /修改程序:增加节点。用头插法,返回头指针/=主函数=void main()char ch10,num5;LinkList head;head=CreatList(); /用头插入法建立单链表,返回头指针printlist(head)

4、; /遍历链表输出其值printf( Delete node (y/n):); /输入y或n去选择是否删除结点scanf(%s,num);if(strcmp(num,y)=0 | strcmp(num,Y)=0)printf(Please input Delete_data:);scanf(%s,ch); /输入要删除的字符串DeleteList(head,ch);printlist(head);printf( Add node ? (y/n):); /输入y或n去选择是否增加结点scanf(%s,num);if(strcmp(num,y)=0 | strcmp(num,Y)=0)head=A

5、ddNode(head);printlist(head);DeleteAll(head); /删除所有结点,释放内存/=用尾插入法建立带头结点的单链表=LinkList CreatListR1(void) char ch10; LinkList head=(LinkList)malloc(sizeof(ListNode); /生成头结点 ListNode *s,*r,*pp; r=head; r-next=NULL; printf(Input # to end ); /输入#代表输入结束 printf(nPlease input Node_data:); scanf(%s,ch); /输入各结

6、点的字符串 while(strcmp(ch,#)!=0) pp=LocateNode(head,ch); /按值查找结点,返回结点指针if(pp=NULL) /没有重复的字符串,插入到链表中s=(ListNode *)malloc(sizeof(ListNode);strcpy(s-data,ch);r-next=s;r=s;r-next=NULL;printf(Input # to end );printf(Please input Node_data:);scanf(%s,ch); return head; /返回头指针/=用头插入法建立带头结点的单链表=LinkList CreatLis

7、t(void)char ch100;LinkList head,p;head=(LinkList)malloc(sizeof(ListNode); head-next=NULL;while(1)printf(Input # to end ); printf(Please input Node_data:);scanf(%s,ch); if(strcmp(ch,#) if(LocateNode(head,ch)=NULL) strcpy(head-data,ch);p=(LinkList)malloc(sizeof(ListNode); p-next=head;head=p;else break

8、;return head; /=按值查找结点,找到则返回该结点的位置,否则返回NULL=ListNode *LocateNode(LinkList head, char *key) ListNode *p=head-next; /从开始结点比较 while(p!=NULL & strcmp(p-data,key)!=0) /直到p为NULL或p-data为key止p=p-next; /扫描下一个结点 return p; /若p=NULL则查找失败,否则p指向找到的值为key的结点/=修改程序:增加节点=ListNode * AddNode(LinkList head) char ch10;Li

9、stNode *s,*pp; printf(nPlease input a New Node_data:); scanf(%s,ch); /输入各结点的字符串pp=LocateNode(head,ch); /按值查找结点,返回结点指针printf(ok2n);if(pp=NULL) /没有重复的字符串,插入到链表中s=(ListNode *)malloc(sizeof(ListNode);strcpy(s-data,ch);printf(ok3n);s-next=head-next;head-next=s;return head;/=删除带头结点的单链表中的指定结点=void DeleteLi

10、st(LinkList head,char *key) ListNode *p,*r,*q=head; p=LocateNode(head,key); /按key值查找结点的 if(p=NULL ) /若没有找到结点,退出printf(position error);exit(0); while(q-next!=p) /p为要删除的结点,q为p的前结点q=q-next; r=q-next; q-next=r-next; free(r); /释放结点/=打印链表=void printlist(LinkList head) ListNode *p=head-next; /从开始结点打印 while

11、(p)printf(%s, ,p-data);p=p-next; printf(n);/=删除所有结点,释放空间=void DeleteAll(LinkList head) ListNode *p=head,*r; while(p-next)r=p-next;free(p);p=r;free(p);实验结果:Input # to end Please input Node_data:batInput # to end Please input Node_data:catInput # to end Please input Node_data:eatInput # to end Please

12、input Node_data:fatInput # to end Please input Node_data:hatInput # to end Please input Node_data:jatInput # to end Please input Node_data:latInput # to end Please input Node_data:matInput # to end Please input Node_data:#mat, lat, jat, hat, fat, eat, cat, bat, Delete node (y/n):yPlease input Delete

13、_data:hatmat, lat, jat, fat, eat, cat, bat, Insert node (y/n):yPlease input Insert_data:putposition :5mat, lat, jat, fat, eat, put, cat, bat,请按任意键继续. . .示意图:latjathatfateatcatbatmatNULLheadlatjathatfateatcatbatmatheadlatjatfateatputcatbatmatheadNULLNULL心得体会:本次实验使我们对链表的实质了解更加明确了,对链表的一些基本操作也更加熟练了。另外实验

14、指导书上给出的代码是有一些问题的,这使我们认识到实验过程中不能想当然的直接编译执行,应当在阅读并完全理解代码的基础上再执行,这才是实验的意义所在。实验2实验题目:二叉树操作设计和实现实验目的:掌握二叉树的定义、性质及存储方式,各种遍历算法。实验要求:采用二叉树链表作为存储结构,完成二叉树的建立,先序、中序和后序以及按层次遍历的操作,求所有叶子及结点总数的操作。实验主要步骤:1、 分析、理解程序。2、 调试程序,设计一棵二叉树,输入完全二叉树的先序序列,用#代表虚结点(空指针),如ABD#CE#F#,建立二叉树,求出先序、中序和后序以及按层次遍历序列,求所有叶子及结点总数。实验代码#includ

15、estdio.h#includestdlib.h#includestring.h#define Max 20 /结点的最大个数typedef struct node char data; struct node *lchild,*rchild;BinTNode; /自定义二叉树的结点类型typedef BinTNode *BinTree; /定义二叉树的指针int NodeNum,leaf; /NodeNum为结点数,leaf为叶子数/=基于先序遍历算法创建二叉树=/=要求输入先序序列,其中加入虚结点#以示空指针的位置=BinTree CreatBinTree(void) BinTree T;

16、 char ch; if(ch=getchar()=#)return(NULL); /读入#,返回空指针 else T= (BinTNode *)malloc(sizeof(BinTNode); /生成结点T-data=ch;T-lchild=CreatBinTree(); /构造左子树T-rchild=CreatBinTree(); /构造右子树return(T); /=NLR 先序遍历=void Preorder(BinTree T) if(T) printf(%c,T-data); /访问结点Preorder(T-lchild); /先序遍历左子树Preorder(T-rchild);

17、/先序遍历右子树 /=LNR 中序遍历= void Inorder(BinTree T) if(T) Inorder(T-lchild); /中序遍历左子树printf(%c,T-data); /访问结点Inorder(T-rchild); /中序遍历右子树 /=LRN 后序遍历=void Postorder(BinTree T) if(T) Postorder(T-lchild); /后序遍历左子树Postorder(T-rchild); /后序遍历右子树printf(%c,T-data); /访问结点 /=采用后序遍历求二叉树的深度、结点数及叶子数的递归算法=int TreeDepth(B

18、inTree T) int hl,hr,max; if(T)hl=TreeDepth(T-lchild); /求左深度hr=TreeDepth(T-rchild); /求右深度max=hlhr? hl:hr; /取左右深度的最大值NodeNum=NodeNum+1; /求结点数if(hl=0&hr=0) leaf=leaf+1; /若左右深度为0,即为叶子。return(max+1); else return(0);/=利用先进先出(FIFO)队列,按层次遍历二叉树=void Levelorder(BinTree T) int front=0,rear=1; BinTNode *cqMax,*

19、p; /定义结点的指针数组cq cq1=T; /根入队 while(front!=rear) front=(front+1)%NodeNum;p=cqfront; /出队printf(%c,p-data); /出队,输出结点的值 if(p-lchild!=NULL) rear=(rear+1)%NodeNum; cqrear=p-lchild; /左子树入队if(p-rchild!=NULL) rear=(rear+1)%NodeNum; cqrear=p-rchild; /右子树入队 /=数叶子节点个数=int countleaf(BinTree T)int hl,hr; if(T)hl=c

20、ountleaf(T-lchild);hr=countleaf(T-rchild);if(hl=0&hr=0) /若左右深度为0,即为叶子。return(1);else return hl+hr; else return 0;/=主函数=void main() BinTree root;char i; int depth; printf(n);printf(Creat Bin_Tree; Input preorder:); /输入完全二叉树的先序序列, / 用#代表虚结点,如ABD#CE#F# root=CreatBinTree(); /创建二叉树,返回根结点 do /从菜单中选择遍历方式,输

21、入序号。printf(t* select *n);printf(t1: Preorder Traversaln); printf(t2: Iorder Traversaln);printf(t3: Postorder traversaln);printf(t4: PostTreeDepth,Node number,Leaf numbern);printf(t5: Level Depthn); /按层次遍历之前,先选择4,求出该树的结点数。printf(t0: Exitn);printf(t*n);fflush(stdin);scanf(%c,&i); /输入菜单序号(0-5)switch (i

22、-0)case 1: printf(Print Bin_tree Preorder: );Preorder(root); /先序遍历break;case 2: printf(Print Bin_Tree Inorder: );Inorder(root); /中序遍历break;case 3: printf(Print Bin_Tree Postorder: );Postorder(root); /后序遍历break;case 4: depth=TreeDepth(root); /求树的深度及叶子数printf(BinTree Depth=%d BinTree Node number=%d,de

23、pth,NodeNum);printf( BinTree Leaf number=%d,countleaf(root);break;case 5: printf(LevePrint Bin_Tree: );Levelorder(root); /按层次遍历break;default: exit(1);printf(n); while(i!=0); 实验结果:Creat Bin_Tree; Input preorder:ABD#CE#F# * select * 1: Preorder Traversal 2: Iorder Traversal 3: Postorder traversal 4: P

24、ostTreeDepth,Node number,Leaf number 5: Level Depth 0: Exit * 1 Print Bin_tree Preorder: ABDCEF 2 Print Bin_Tree Inorder: DBAECF 3 Print Bin_Tree Postorder: DBEFCA 4 BinTree Depth=3 BinTree Node number=6 BinTree Leaf number=3 5 LevePrint Bin_Tree: ABCDEF 0 Press any key to continue 二叉树示意图ABFEDC心得体会:

25、这次实验加深了我对二叉树的印象,尤其是对二叉树的各种遍历操作有了一定的了解。同时认识到,在设计程序时辅以图形化的描述是非常有用处的。实验3实验题目:图的遍历操作实验目的:掌握有向图和无向图的概念;掌握邻接矩阵和邻接链表建立图的存储结构;掌握DFS及BFS对图的遍历操作;了解图结构在人工智能、工程等领域的广泛应用。实验要求:采用邻接矩阵和邻接链表作为图的存储结构,完成有向图和无向图的DFS和BFS操作。实验主要步骤:设计一个有向图和一个无向图,任选一种存储结构,完成有向图和无向图的DFS(深度优先遍历)和BFS(广度优先遍历)的操作。1 邻接矩阵作为存储结构#includestdio.h#inc

26、ludestdlib.h#define MaxVertexNum 100 /定义最大顶点数typedef struct char vexsMaxVertexNum; /顶点表 int edgesMaxVertexNumMaxVertexNum; /邻接矩阵,可看作边表 int n,e; /图中的顶点数n和边数eMGraph; /用邻接矩阵表示的图的类型/=建立邻接矩阵=void CreatMGraph(MGraph *G) int i,j,k; char a; printf(Input VertexNum(n) and EdgesNum(e): ); scanf(%d,%d,&G-n,&G-e

27、); /输入顶点数和边数 scanf(%c,&a); printf(Input Vertex string:); for(i=0;in;i+) scanf(%c,&a); G-vexsi=a; /读入顶点信息,建立顶点表 for(i=0;in;i+)for(j=0;jn;j+) G-edgesij=0; /初始化邻接矩阵 printf(Input edges,Creat Adjacency Matrixn); for(k=0;ke;k+) /读入e条边,建立邻接矩阵 scanf(%d%d,&i,&j); /输入边(Vi,Vj)的顶点序号 G-edgesij=1; G-edgesji=1; /若

28、为无向图,矩阵为对称矩阵;若建立有向图,去掉该条语句 /=定义标志向量,为全局变量=typedef enumFALSE,TRUE Boolean;Boolean visitedMaxVertexNum;/=DFS:深度优先遍历的递归算法=void DFSM(MGraph *G,int i) /以Vi为出发点对邻接矩阵表示的图G进行DFS搜索,邻接矩阵是0,1矩阵 int j; printf(%c,G-vexsi); /访问顶点Vi visitedi=TRUE; /置已访问标志 for(j=0;jn;j+) /依次搜索Vi的邻接点if(G-edgesij=1 & ! visitedj) DFSM

29、(G,j); /(Vi,Vj)E,且Vj未访问过,故Vj为新出发点void DFS(MGraph *G) int i; for(i=0;in;i+)visitedi=FALSE; /标志向量初始化 for(i=0;in;i+)if(!visitedi) /Vi未访问过 DFSM(G,i); /以Vi为源点开始DFS搜索/=BFS:广度优先遍历=void BFS(MGraph *G,int k) /以Vk为源点对用邻接矩阵表示的图G进行广度优先搜索 int i,j,f=0,r=0; int cqMaxVertexNum; /定义队列 for(i=0;in;i+)visitedi=FALSE; /

30、标志向量初始化 for(i=0;in;i+)cqi=-1; /队列初始化 printf(%c,G-vexsk); /访问源点Vk visitedk=TRUE; cqr=k; /Vk已访问,将其入队。注意,实际上是将其序号入队 while(cqf!=-1) /队非空则执行 i=cqf; f=f+1; /Vf出队 for(j=0;jn;j+) /依次Vi的邻接点Vj if(G-edgesij=1 & !visitedj) /Vj未访问 printf(%c,G-vexsj); /访问Vj visitedj=TRUE; r=r+1; cqr=j; /访问过Vj入队 /=main=void main()

31、 int i; MGraph *G; G=(MGraph *)malloc(sizeof(MGraph); /为图G申请内存空间 CreatMGraph(G); /建立邻接矩阵 printf(Print Graph DFS: ); DFS(G); /深度优先遍历 printf(n); printf(Print Graph BFS: ); BFS(G,3); /以序号为3的顶点开始广度优先遍历 printf(n);2 邻接链表作为存储结构#includestdio.h#includestdlib.h#define MaxVertexNum 50 /定义最大顶点数typedef struct no

32、de /边表结点 int adjvex; /邻接点域 struct node *next; /链域EdgeNode;typedef struct vnode /顶点表结点 char vertex; /顶点域 EdgeNode *firstedge; /边表头指针VertexNode;typedef VertexNode AdjListMaxVertexNum; /AdjList是邻接表类型typedef struct AdjList adjlist; /邻接表 int n,e; /图中当前顶点数和边数 ALGraph; /图类型/=建立图的邻接表=void CreatALGraph(ALGra

33、ph *G) int i,j,k; char a; EdgeNode *s; /定义边表结点 printf(Input VertexNum(n) and EdgesNum(e): ); scanf(%d,%d,&G-n,&G-e); /读入顶点数和边数 scanf(%c,&a); printf(Input Vertex string:); for(i=0;in;i+) /建立边表 scanf(%c,&a);G-adjlisti.vertex=a; /读入顶点信息G-adjlisti.firstedge=NULL; /边表置为空表 printf(Input edges,Creat Adjacency Listn); for(k=0;ke;k+) /建立边表 scanf(%d%d,&i,&j); /读入边(Vi,Vj)的顶点对序号s=(EdgeNode *)malloc(sizeof(EdgeNode); /生成边表结点s-adjvex=j; /邻接点序号为js-next=G-adjlisti.firstedge;G-adjlisti.firstedge=s; /将新结点*S插入顶点Vi的边表头部s=(EdgeNode *)malloc(si

展开阅读全文
相关资源
相关搜索

当前位置:首页 > 技术资料 > 技术方案

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

工信部备案号:黑ICP备15003705号-8 |  经营许可证:黑B2-20190332号 |   黑公网安备:91230400333293403D

© 2020-2023 www.deliwenku.com 得利文库. All Rights Reserved 黑龙江转换宝科技有限公司 

黑龙江省互联网违法和不良信息举报
举报电话:0468-3380021 邮箱:hgswwxb@163.com