数据结构实验报告---答案汇总(共25页)_第1页
数据结构实验报告---答案汇总(共25页)_第2页
数据结构实验报告---答案汇总(共25页)_第3页
数据结构实验报告---答案汇总(共25页)_第4页
数据结构实验报告---答案汇总(共25页)_第5页
已阅读5页,还剩21页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

1、精选优质文档-倾情为你奉上数据结构(C语言版) 实验报告专业 班级 学号 姓名 实验1实验题目:单链表的插入和删除实验目的:了解和掌握线性表的逻辑结构和链式存储结构,掌握单链表的基本算法及相关的时间性能分析。实验要求:建立一个数据域定义为字符串的单链表,在链表中不允许有重复的字符串;根据输入的字符串,先找到相应的结点,后删除之。实验主要步骤:1、 分析、理解给出的示例程序。2、 调试程序,并设计输入数据(如:bat,cat,eat,fat,hat,jat,lat,mat,#),测试程序的如下功能:不允许重复字符串的插入;根据输入的字符串,找到相应的结点并删除。3、 修改程序:(1) 增加插入结

2、点的功能。(2) 将建立链表的方法改为头插入法。程序代码:#include"stdio.h"#include"string.h"#include"stdlib.h"#include"ctype.h"typedef struct node /定义结点char data10; /结点的数据域为字符串struct node *next; /结点的指针域ListNode;typedef ListNode * LinkList; / 自定义LinkList单链表类型LinkList CreatListR1(); /函数,用尾

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

4、建立单链表,返回头指针printlist(head); /遍历链表输出其值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(hea

5、d,ch);printlist(head);printf(" Add node ? (y/n):"); /输入"y"或"n"去选择是否增加结点scanf("%s",num);if(strcmp(num,"y")=0 | strcmp(num,"Y")=0)head=AddNode(head);printlist(head);DeleteAll(head); /删除所有结点,释放内存/=用尾插入法建立带头结点的单链表=LinkList CreatListR1(void) cha

6、r 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); /输入各结点的字符串 while(strcmp(ch,"#")!=0) pp=LocateNode

7、(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 Cr

8、eatList(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=(L

9、inkList)malloc(sizeof(ListNode); p->next=head;head=p;else break;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

10、p; /若p=NULL则查找失败,否则p指向找到的值为key的结点/=修改程序:增加节点=ListNode * AddNode(LinkList head) char ch10;ListNode *s,*pp; printf("nPlease input a New Node_data:"); scanf("%s",ch); /输入各结点的字符串pp=LocateNode(head,ch); /按值查找结点,返回结点指针printf("ok2n");if(pp=NULL) /没有重复的字符串,插入到链表中s=(ListNode *)m

11、alloc(sizeof(ListNode);strcpy(s->data,ch);printf("ok3n");s->next=head->next;head->next=s;return head;/=删除带头结点的单链表中的指定结点=void DeleteList(LinkList head,char *key) ListNode *p,*r,*q=head; p=LocateNode(head,key); /按key值查找结点的 if(p=NULL ) /若没有找到结点,退出printf("position error")

12、;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(p)printf("%s, ",p->data);p=p->next; printf("n");/=删除所有结点,释放空间=void DeleteAll(LinkL

13、ist 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 input Node_data:fatInput # to end Please input Node_data:hatInput # t

14、o 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_data:hatmat, lat, jat, fat, eat, cat, bat, Insert node (y/n):yPlease

15、 input Insert_data:putposition :5mat, lat, jat, fat, eat, put, cat, bat,请按任意键继续. . .示意图:latjathatfateatcatbatmatNULLheadlatjathatfateatcatbatmatheadlatjatfateatputcatbatmatheadNULLNULL心得体会:本次实验使我们对链表的实质了解更加明确了,对链表的一些基本操作也更加熟练了。另外实验指导书上给出的代码是有一些问题的,这使我们认识到实验过程中不能想当然的直接编译执行,应当在阅读并完全理解代码的基础上再执行,这才是实验的意

16、义所在。实验2实验题目:二叉树操作设计和实现实验目的:掌握二叉树的定义、性质及存储方式,各种遍历算法。实验要求:采用二叉树链表作为存储结构,完成二叉树的建立,先序、中序和后序以及按层次遍历的操作,求所有叶子及结点总数的操作。实验主要步骤:1、 分析、理解程序。2、 调试程序,设计一棵二叉树,输入完全二叉树的先序序列,用#代表虚结点(空指针),如ABD#CE#F#,建立二叉树,求出先序、中序和后序以及按层次遍历序列,求所有叶子及结点总数。实验代码#include"stdio.h"#include"stdlib.h"#include"string.

17、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; char ch; if(ch=getch

18、ar()='#')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); /先序遍历左子

19、树Preorder(T->rchild); /先序遍历右子树 /=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("

20、;%c",T->data); /访问结点 /=采用后序遍历求二叉树的深度、结点数及叶子数的递归算法=int TreeDepth(BinTree T) int hl,hr,max; if(T)hl=TreeDepth(T->lchild); /求左深度hr=TreeDepth(T->rchild); /求右深度max=hl>hr? hl:hr; /取左右深度的最大值NodeNum=NodeNum+1; /求结点数if(hl=0&&hr=0) leaf=leaf+1; /若左右深度为0,即为叶子。return(max+1); else return

21、(0);/=利用"先进先出"(FIFO)队列,按层次遍历二叉树=void Levelorder(BinTree T) int front=0,rear=1; BinTNode *cqMax,*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->lchi

22、ld; /左子树入队if(p->rchild!=NULL) rear=(rear+1)%NodeNum; cqrear=p->rchild; /右子树入队 /=数叶子节点个数=int countleaf(BinTree T)int hl,hr; if(T)hl=countleaf(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;c

23、har i; int depth; printf("n");printf("Creat Bin_Tree; Input preorder:"); /输入完全二叉树的先序序列, / 用#代表虚结点,如ABD#CE#F# root=CreatBinTree(); /创建二叉树,返回根结点 do /从菜单中选择遍历方式,输入序号。printf("t* select *n");printf("t1: Preorder Traversaln"); printf("t2: Iorder Traversaln"

24、;);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-'0&#

25、39;)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("

26、;BinTree Depth=%d BinTree Node number=%d",depth,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#C

27、E#F# * select * 1: Preorder Traversal 2: Iorder Traversal 3: Postorder traversal 4: PostTreeDepth,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 Lea

28、f number=3 5 LevePrint Bin_Tree: ABCDEF 0 Press any key to continue 二叉树示意图ABFEDC心得体会:这次实验加深了我对二叉树的印象,尤其是对二叉树的各种遍历操作有了一定的了解。同时认识到,在设计程序时辅以图形化的描述是非常有用处的。实验3实验题目:图的遍历操作实验目的:掌握有向图和无向图的概念;掌握邻接矩阵和邻接链表建立图的存储结构;掌握DFS及BFS对图的遍历操作;了解图结构在人工智能、工程等领域的广泛应用。实验要求:采用邻接矩阵和邻接链表作为图的存储结构,完成有向图和无向图的DFS和BFS操作。实验主要步骤:设计一个有向

29、图和一个无向图,任选一种存储结构,完成有向图和无向图的DFS(深度优先遍历)和BFS(广度优先遍历)的操作。1 邻接矩阵作为存储结构#include"stdio.h"#include"stdlib.h"#define MaxVertexNum 100 /定义最大顶点数typedef struct char vexsMaxVertexNum; /顶点表 int edgesMaxVertexNumMaxVertexNum; /邻接矩阵,可看作边表 int n,e; /图中的顶点数n和边数eMGraph; /用邻接矩阵表示的图的类型/=建立邻接矩阵=void

30、CreatMGraph(MGraph *G) int i,j,k; char a; printf("Input VertexNum(n) and EdgesNum(e): "); scanf("%d,%d",&G->n,&G->e); /输入顶点数和边数 scanf("%c",&a); printf("Input Vertex string:"); for(i=0;i<G->n;i+) scanf("%c",&a); G->vexsi

31、=a; /读入顶点信息,建立顶点表 for(i=0;i<G->n;i+)for(j=0;j<G->n;j+) G->edgesij=0; /初始化邻接矩阵 printf("Input edges,Creat Adjacency Matrixn"); for(k=0;k<G->e;k+) /读入e条边,建立邻接矩阵 scanf("%d%d",&i,&j); /输入边(Vi,Vj)的顶点序号 G->edgesij=1; G->edgesji=1; /若为无向图,矩阵为对称矩阵;若建立有向图

32、,去掉该条语句 /=定义标志向量,为全局变量=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;j<G->n;j+) /依次搜索Vi的邻接点if(G->edgesij=1 &&a

33、mp; ! visitedj) DFSM(G,j); /(Vi,Vj)E,且Vj未访问过,故Vj为新出发点void DFS(MGraph *G) int i; for(i=0;i<G->n;i+)visitedi=FALSE; /标志向量初始化 for(i=0;i<G->n;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 cqMaxVertexNu

34、m; /定义队列 for(i=0;i<G->n;i+)visitedi=FALSE; /标志向量初始化 for(i=0;i<G->n;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;j<G->n;j+) /依次Vi的邻接点Vj if(G->edgesij=1 && !

35、visitedj) /Vj未访问 printf("%c",G->vexsj); /访问Vj visitedj=TRUE; r=r+1; cqr=j; /访问过Vj入队 /=main=void main() int i; MGraph *G; G=(MGraph *)malloc(sizeof(MGraph); /为图G申请内存空间 CreatMGraph(G); /建立邻接矩阵 printf("Print Graph DFS: "); DFS(G); /深度优先遍历 printf("n"); printf("Print

36、 Graph BFS: "); BFS(G,3); /以序号为3的顶点开始广度优先遍历 printf("n");2 邻接链表作为存储结构#include"stdio.h"#include"stdlib.h"#define MaxVertexNum 50 /定义最大顶点数typedef struct node /边表结点 int adjvex; /邻接点域 struct node *next; /链域EdgeNode;typedef struct vnode /顶点表结点 char vertex; /顶点域 EdgeNode

37、*firstedge; /边表头指针VertexNode;typedef VertexNode AdjListMaxVertexNum; /AdjList是邻接表类型typedef struct AdjList adjlist; /邻接表 int n,e; /图中当前顶点数和边数 ALGraph; /图类型/=建立图的邻接表=void CreatALGraph(ALGraph *G) int i,j,k; char a; EdgeNode *s; /定义边表结点 printf("Input VertexNum(n) and EdgesNum(e): "); scanf(&q

38、uot;%d,%d",&G->n,&G->e); /读入顶点数和边数 scanf("%c",&a); printf("Input Vertex string:"); for(i=0;i<G->n;i+) /建立边表 scanf("%c",&a);G->adjlisti.vertex=a; /读入顶点信息G->adjlisti.firstedge=NULL; /边表置为空表 printf("Input edges,Creat Adjacency Li

39、stn"); for(k=0;k<G->e;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(sizeof(EdgeNode); s->adjv

40、ex=i; /邻接点序号为is->next=G->adjlistj.firstedge; G->adjlistj.firstedge=s; /将新结点*S插入顶点Vj的边表头部 /=定义标志向量,为全局变量=typedef enumFALSE,TRUE Boolean;Boolean visitedMaxVertexNum;/=DFS:深度优先遍历的递归算法=void DFSM(ALGraph *G,int i) /以Vi为出发点对邻接链表表示的图G进行DFS搜索 EdgeNode *p; printf("%c",G->adjlisti.vertex

41、); /访问顶点Vi visitedi=TRUE; /标记Vi已访问 p=G->adjlisti.firstedge; /取Vi边表的头指针 while(p) /依次搜索Vi的邻接点Vj,这里j=p->adjvexif(! visitedp->adjvex) /若Vj尚未被访问 DFSM(G,p->adjvex); /则以Vj为出发点向纵深搜索p=p->next; /找Vi的下一个邻接点 void DFS(ALGraph *G) int i; for(i=0;i<G->n;i+)visitedi=FALSE; /标志向量初始化 for(i=0;i<

42、;G->n;i+)if(!visitedi) /Vi未访问过 DFSM(G,i); /以Vi为源点开始DFS搜索/=BFS:广度优先遍历=void BFS(ALGraph *G,int k) /以Vk为源点对用邻接链表表示的图G进行广度优先搜索 int i,f=0,r=0; EdgeNode *p; int cqMaxVertexNum; /定义FIFO队列 for(i=0;i<G->n;i+)visitedi=FALSE; /标志向量初始化 for(i=0;i<=G->n;i+)cqi=-1; /初始化标志向量 printf("%c",G-&

43、gt;adjlistk.vertex); /访问源点Vk visitedk=TRUE; cqr=k; /Vk已访问,将其入队。注意,实际上是将其序号入队 while(cqf!=-1) 队列非空则执行i=cqf; f=f+1; /Vi出队p=G->adjlisti.firstedge; /取Vi的边表头指针while(p) /依次搜索Vi的邻接点Vj(令p->adjvex=j) if(!visitedp->adjvex) /若Vj未访问过printf("%c",G->adjlistp->adjvex.vertex); /访问Vjvisitedp-

44、>adjvex=TRUE;r=r+1; cqr=p->adjvex; /访问过的Vj入队 p=p->next; /找Vi的下一个邻接点 /endwhile/=主函数=void main() int i; ALGraph *G; G=(ALGraph *)malloc(sizeof(ALGraph); CreatALGraph(G); printf("Print Graph DFS: "); DFS(G); printf("n"); printf("Print Graph BFS: "); BFS(G,3); prin

45、tf("n");实验结果:1. 邻接矩阵作为存储结构执行顺序:V6V4V5V7V2V3V1V0VoInput VertexNum(n) and EdgesNum(e): 8,9Input Vertex string: Input edges,Creat Adjacency Matrix0 10 21 31 42 52 63 74 75 6Print Graph DFS: Print Graph BFS: 2. 邻接链表作为存储结构执行顺序:Input VertexNum(n) and EdgesNum(e): 8,9Input Vertex string: V6V4V5V7V2V3V1V0VoInput edges,Creat Adjacency List0 10 21 31 42 52 63 74 75 6Print Graph DFS: Print Graph BFS: 心得体会:这次实验较以前的实验难度加大,必须先理解深度优先和广度优先两种遍历思路,和数据结构中队列的基本操作,才能看懂理解代码。同时图这种数据结构对抽象的能力要求非常高,代码不容易看懂,排错也比较麻烦,应该多加练习,才能掌握。实验4 实验题目:排序实验目的:掌握各种

温馨提示

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

评论

0/150

提交评论