1、完整word版数据结构实验报告本科实验报告课程名称: 数据结构 实验项目: 线性表 树 图 查找 排序 实验地点: 专业班级: 学号: 学生姓名: 指导教师: 年 月 日实验名称 实验一 线性表实验目的和要求本次实习的主要目的是为了使学生熟练掌握线性表的基本操作在顺序存储结构和链式存储结构上的实现,提高分析和解决问题的能力.要求仔细阅读并理解下列例题,上机通过,并观察其结果,然后独立完成后面的实习题。实验内容1设顺序表A中的数据元素递增有序,试写一程序,将x插入到顺序表的适当位置上,使该表仍然有序.2用单链表ha 存储多项式A(x )=a0+a1x1+a2x2+anxn(其中aI为非零系数),
2、用单链表hb 存储多项式B(x )=b0+b1x1+b2x2+bmxm(其中bj为非零系数),要求计算C(x )= A(x )+B(x ),结果存到单链表hc中。试写出程序。 3设有n个人围坐在一个圆桌周围,现从第s个人开始报数,数到第m的人出列,然后从出列的下一个人重新开始报数,数到m的人又出列,如此重复,直到所有的人全部出列为止。Josephus问题是:对于任意给定的n,m,s,求出按出列次序得到的n个人员的顺序表。主要仪器设备台式或笔记本计算机实验记录(可分栏或加页)#includestdio。h#includestdlib。hinclude struct node char info;
3、struct node llink,*rlink; ; typedef struct node NODE; NODE *creat() char x; NODE p; scanf(”c,x); printf(c”,x); if(x!=.) p=(NODE)malloc(sizeof(NODE); pinfo=x; p-llink=creat(); prlink=creat(); else p=NULL; return p; void countleaf(NODEt,int &count) if(t) if(!t-llink)&(!t-rlink)) count+; countleaf(tlli
4、nk,count); countleaf(trlink,count); int main() int e=0; NODE *T; printf(qing shu ru er cha shu ”); T=creat(); printf(n); countleaf(T,e); printf(dn”,e); system(PAUSE); 实验结果:includestdio。hincludetypedefstructdxsinta;structdxs*next;Dxs,Dxss;voidStructure(Dxsshead,intn);voidShow(Dxsshead);voidAdd(Dxsshe
5、ad1,Dxsshead2,Dxsshead3);voidfrees(Dxsshead);voidmain()Dxssha,hb,hc;intn;ha=(Dxss)malloc(sizeof(Dxs);hb=(Dxss)malloc(sizeof(Dxs);hc=(Dxss)malloc(sizeof(Dxs);printf(请输入多项式1的项数n”);scanf(”%d”,n);Structure(ha,n);printf(请输入多项式2的项数n);scanf(d,&n);Structure(hb,n);Add(ha,hb,hc);printf(”多项式HC的式子是n”);Show(hc);
6、frees(ha);frees(hb);frees(hc);printf(”nn”);voidStructure(Dxsshead,intn)Dxssp,q;inta;printf(请输入要录入系统的多项式的系数,从次数较小的开始n”);p=head;doscanf(%d,a);q=(Dxss)malloc(sizeof(Dxs));q-a=a;p-next=q;q-next=NULL;p=q;while(n);voidShow(Dxsshead)intm=0;Dxssp;p=headnext;while(p!=NULL)printf(”d*Xd,pa,m+);p=pnext;if(p!=NU
7、LL)printf(”+);printf(”n);voidAdd(Dxsshead1,Dxsshead2,Dxsshead3)Dxssp,q,l,m;p=head1next;q=head2next;l=head3;while((p!=NULL)(q!=NULL))m=(Dxss)malloc(sizeof(Dxs);ma=pa;lnext=m;l=m;p=p-next;lnext=NULL;voidfrees(Dxsshead)Dxssp,q;p=head;while(p!=NULL)q=p;p=pnext;free(q);实验结果:心得:通过这次试验让我认识到了要注意细节,否则很容易出错实验
8、名称 实验二 树实验目的和要求熟悉树的各种表示方法和各种遍历方式,掌握有关算法的实现,了解树在计 算机科学及其它工程技术中的应用。实验内容问题描述 任意给定一棵二叉树。试设计一个程序,在计算机中构造该二叉树,并对它 进行遍历.输入一棵二叉树的结点若无子树,则可将其子树看作 “。”,输入时,按照前序序列的顺序输入该结点的内容。对 下图,其输入序列为ABD.。EH。CF.I.。G。输出若为空二叉树,则输出:THIS IS A EMPTY BINARY TREE。若二叉树不空,按后序序列输出,对上例,输出结果为:DHEBIFGCA.存储结构采用二叉链表存储。实习题1 编写递归算法,计算二叉树中叶子结
9、点的数目。2 编写递归算法,在二叉树中求位于先序序列中第K个位置的结点.3 将上述例题用非递归程序实现。主要仪器设备台式或笔记本计算机实验记录(可分栏或加页)#includestdio。hincludestdlib。hincludemalloc。hintcount=0;structnodecharinfo;structnodellink,*rlink;typedefstructnodeNODE;NODEcreat()charx;NODE*p;scanf(”c,x);printf(c”,x);if(x!=.)p=(NODE*)malloc(sizeof(NODE);p-info=x;p-llin
10、k=creat();prlink=creat();elsep=NULL;returnp;voidrun(NODEt)if(t)run(t-llink);run(trlink);printf(”c,tinfo);if((t-llink)=NULL)&((t-rlink)=NULL))count+;voidmain()NODE*T;printf(qingshuruerchashu:n”);T=creat();printf(n”);if(!T)printf(Thisisaemptybinarytree”);elseprintf(”Theresultofposttraveseis:n”);run(T)
11、;printf(”总共有叶子结点数%d”,count);printf(n);实验结果:心得:树是数据结构非常重要的部分,要很好地掌握实验名称 实验三 图实验目的和要求熟悉图的存储结构,掌握有关算法的实现,了解图在计算机科学及其他工程技术中的应用。实验内容采用邻接表存储结构,编写一个求无向图的连通分量个数的算法。主要仪器设备台式或笔记本计算机实验记录includestdio。h#includeintn;structVNodeintposition;structVNodenext;;structArcNodeintmark;structVNodefirst;;voidDFS(struct ArcN
12、ode*v,structArcNodew)structVNodeL;wmark=1;L=w-first;while(L!=NULL)if((v+(Lposition)-mark=0)DFS(v,(v+L-position));L=Lnext;intmain()inti,j,k; intnum=0; structArcNode*p; structVNodetemp;structVNodeflag; printf(n请输入顶点个数n:); scanf(d,&n);while(n1)printf(你输入的值不合理,请重新输入:n);scanf(”d,n);p=(structArcNode)mallo
13、c(nsizeof(structArcNode));for(i=0;in;i+)printf(n请输入以Vd为弧尾的所有弧,并以-1结束输入n”,i+1);scanf(”%d,&k); if(k=-1)pi。mark=0; pi.first=NULL; elsetemp=(structVNode)malloc(sizeof(structVNode);temp-position=k; temp-next=NULL; pi.first=temp;pi.mark=0; flag=temp; scanf(%d,k); while(k!=1)temp=(structVNode)malloc(sizeof
14、(structVNode);temp-position=k; temp-next=NULL; flag-next=temp;flag=temp; scanf(%d,k);i=0; while(pi。mark=0)DFS(p,(p+i)); num+; i=0;while(pi。mark!=0in) i+;printf(此图的连通分量个数为:dn”,num);system(”pause); return0;实验结果:心得:图比较复杂,要注意算法的复杂度,减少程序代码实验名称 实验四 查找实验目的和要求:实验内容主要仪器设备台式或笔记本计算机实验记录(可分栏或加页) #include ”stdio
15、.h”typedef structint a30; int length;sqtable;sqtable st;int b=0;void createst(int k)int i; printf(Please input data:”); st。a0=100; for(i=1;(!b&(i=k);i+) scanf(”%d”,(st.ai); if(st.aist.ai-1) printf(”Input data error.n); b=1; if(!b)st。length=k; printf(”The table is builted。n”); int stfind(sqtable st,i
16、nt l,int h,int y)int m; while(l=h) m=(l+h)/2; if(y=st。am) ;return m; else if (y=0) i+; while(j=0&aj0) j-; if(ij) temp=ai; ai=aj; aj=temp; i+; j-; int main() int n,i; int* p; printf(”你有多少个数据有待整序(必须大于1):n); scanf(”d”,n); while(n2) printf(你输入的数值不合理,请重新输入:n”); scanf(”d”,n); p=(int)malloc(n*sizeof(int); printf(请输入数据:n); for(i=0;in;i+) scanf(”d”,pi); Sort(p,n); printf(”整序后的数据为:n); for(i=0;in;i+) printf(”%4d”,pi); printf(n); system(”pause); return 0; 实验结果:心得:排序过程中应注意细节,循环次数,细节决定成败
copyright@ 2008-2022 冰豆网网站版权所有
经营许可证编号:鄂ICP备2022015515号-1