完整word版数据结构实验报告.docx

上传人:b****1 文档编号:2443511 上传时间:2023-05-03 格式:DOCX 页数:10 大小:86.06KB
下载 相关 举报
完整word版数据结构实验报告.docx_第1页
第1页 / 共10页
完整word版数据结构实验报告.docx_第2页
第2页 / 共10页
完整word版数据结构实验报告.docx_第3页
第3页 / 共10页
完整word版数据结构实验报告.docx_第4页
第4页 / 共10页
完整word版数据结构实验报告.docx_第5页
第5页 / 共10页
完整word版数据结构实验报告.docx_第6页
第6页 / 共10页
完整word版数据结构实验报告.docx_第7页
第7页 / 共10页
完整word版数据结构实验报告.docx_第8页
第8页 / 共10页
完整word版数据结构实验报告.docx_第9页
第9页 / 共10页
完整word版数据结构实验报告.docx_第10页
第10页 / 共10页
亲,该文档总共10页,全部预览完了,如果喜欢就下载吧!
下载资源
资源描述

完整word版数据结构实验报告.docx

《完整word版数据结构实验报告.docx》由会员分享,可在线阅读,更多相关《完整word版数据结构实验报告.docx(10页珍藏版)》请在冰点文库上搜索。

完整word版数据结构实验报告.docx

完整word版数据结构实验报告

本科实验报告

 

课程名称:

数据结构

实验项目:

线性表树图查找排序

实验地点:

专业班级:

学号:

学生姓名:

指导教师:

年月日

实验名称

实验一线性表

实验目的和要求

本次实习的主要目的是为了使学生熟练掌握线性表的基本操作在顺序存储结构和链式存储结构上的实现,提高分析和解决问题的能力.要求仔细阅读并理解下列例题,上机通过,并观察其结果,然后独立完成后面的实习题。

实验内容

1.设顺序表A中的数据元素递增有序,试写一程序,将x插入到顺序表的适当位置上,使该表仍然有序.

2.用单链表ha存储多项式A(x)=a0+a1x1+a2x2+…+anxn(其中aI为非零系数),用单链表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个人员的顺序表。

主要仪器设备

台式或笔记本计算机

实验记录(可分栏或加页)

#include

h〉

#include

h〉

#include

structnode{charinfo;structnode*llink,*rlink;};

typedefstructnodeNODE;NODE*creat(){

charx;NODE*p;scanf(”%c",&x);

printf("%c”,x);if(x!

='.’){

p=(NODE*)malloc(sizeof(NODE));

p—>info=x;

p->llink=creat();

p—>rlink=creat();}

else

p=NULL;returnp;}

voidcountleaf(NODE*t,int&count){

if(t){

if((!

t-〉llink)&&(!

t-〉rlink))

count++;

countleaf(t—〉llink,count);

countleaf(t—>rlink,count);

}}

intmain(){

inte=0;NODE*T;

printf("qingshuruerchashu”);

T=creat();printf("\n");

countleaf(T,e);printf("%d\n”,e);

system("PAUSE");}

实验结果:

#include 

h〉 

#include 

h> 

typedef struct dxs{ 

 int a; struct dxs *next;

 }Dxs, *Dxss;

void Structure( Dxss head, int n ); 

void Show(Dxss head); 

void Add( Dxss head1, Dxss head2, Dxss head3 );  

void frees( Dxss head );  

void main() {

 Dxss ha,hb,hc;

 int n;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); frees(ha); frees(hb); frees(hc); printf(”\n\n”);}

void Structure(Dxss head,int n){

Dxss p,q;int a;

printf("请输入要录入系统的多项式的系数,从次数较小的开始\n”);

p=head;do{scanf("%d",&a);q=(Dxss)malloc(sizeof(Dxs));q->a=a;p-〉next=q;

q-〉next=NULL;p=q;

}while(——n);

void Show(Dxss head){

int m=0;Dxss p;p=head—>next;while(p!

=NULL){

printf(”%d*X^%d",p—>a,m++);p=p—>next;if(p!

=NULL)printf(”+");

printf(”n");}

void Add(Dxss head1,Dxss head2,Dxss head3){

Dxss p,q,l,m;p=head1—>next;q=head2—〉next;l=head3;

while((p!

=NULL)&&(q!

=NULL)){

m=(Dxss)malloc(sizeof(Dxs));

m—>a=p—〉a;l—〉next =m;l=m;p=p->next;

}

l—>next =NULL;}

void frees(Dxss head){

Dxss p,q;p=head;while(p!

=NULL)

{q=p;p=p—〉next;free(q);

}}

实验结果:

心得:

通过这次试验让我认识到了要注意细节,否则很容易出错

实验名称

实验二树

实验目的和要求

熟悉树的各种表示方法和各种遍历方式,掌握有关算法的实现,了解树在计算机科学及其它工程技术中的应用。

实验内容

[问题描述]

任意给定一棵二叉树。

试设计一个程序,在计算机中构造该二叉树,并对它进行遍历.

[输入]

一棵二叉树的结点若无子树,则可将其子树看作“。

”,输入时,按照前序序列的顺序输入该结点的内容。

对下图,其输入序列为ABD.。

EH。

CF.I.。

G。

[输出]

若为空二叉树,则输出:

THIS IS A EMPTY BINARY TREE。

若二叉树不空,按后序序列输出,对上例,输出结果为:

DHEBIFGCA. 

[存储结构]

采用二叉链表存储。

实习题

1编写递归算法,计算二叉树中叶子结点的数目。

2.编写递归算法,在二叉树中求位于先序序列中第K个位置的结点.

3.将上述例题用非递归程序实现。

主要仪器设备

台式或笔记本计算机

实验记录(可分栏或加页)

#include〈stdio。

h>

#include〈stdlib。

h〉

#include〈malloc。

h〉 

int count=0;

struct node{   

char info;  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));  

p-〉info=x;              p-〉llink=creat();    p—〉rlink=creat();            }              

else              

p=NULL;    return p;       }

 void  run(NODE*t){

 if(t){    run(t-〉llink);run(t—>rlink);   printf(”%c",t—>info); 

if(((t-〉llink )=NULL)&&((t-〉rlink)=NULL))

count++;}     } 

void main(){       

 NODE *T;    printf("qing shu ru er cha shu:

\n”); 

 T=creat();      printf("\n”);     if(!

T)

 printf("This is a empty binary tree”);

 else

 {printf(”The result of post travese is:

\n”);run(T);

 } 

 printf(”总共有叶子结点数%d”,count);

 printf("\n");}

实验结果:

心得:

树是数据结构非常重要的部分,要很好地掌握

实验名称

实验三图

实验目的和要求

熟悉图的存储结构,掌握有关算法的实现,了解图在计算机科学及其他工程技术中的应用。

实验内容

采用邻接表存储结构,编写一个求无向图的连通分量个数的算法。

主要仪器设备

台式或笔记本计算机

实验记录

#include〈stdio。

h>

#include

int n;

struct VNode{

int position;struct VNode*next;};

struct ArcNode{int mark;struct VNode*first;};

void DFS(structArcNode*v,struct ArcNode*w){

struct VNode*L;w—〉mark=1;L=w-〉first;while(L!

=NULL){

if((v+(L—〉position))->mark==0){

DFS(v,(v+L->position));}

L=L—>next;}}

int main(){int i,j,k;int num=0;struct ArcNode*p;struct VNode*temp;

struct VNode*flag;printf("\n请输入顶点个数n:

");scanf("%d",&n);

while(n〈1){printf("你输入的值不合理,请重新输入:

\n");

scanf(”%d",&n);}

p=(struct ArcNode*)malloc(n*sizeof(struct ArcNode));

for(i=0;i〈n;i++){

printf("\n请输入以V%d为弧尾的所有弧,并以-1结束输入\n”,i+1);

scanf(”%d",&k);if(k==-1){

p[i]。

mark=0;p[i].first=NULL;}

else{

temp=(struct VNode*)malloc(sizeof(struct VNode));

temp-〉position=k;temp-〉next=NULL;p[i].first=temp;

p[i].mark=0;flag=temp;scanf("%d",&k);while(k!

=—1){

temp=(struct VNode*)malloc(sizeof(struct VNode));

temp-〉position=k;temp->next=NULL;flag->next=temp;

flag=temp;scanf("%d",&k);

}}}

i=0;while(p[i]。

mark==0){

DFS(p,(p+i));num++;i=0;

while(p[i]。

mark!

=0&&i

}}

printf("此图的连通分量个数为:

%d\n”,num);

system(”pause");return 0;

实验结果:

心得:

图比较复杂,要注意算法的复杂度,减少程序代码

实验名称

实验四查找

实验目的和要求:

实验内容

主要仪器设备

台式或笔记本计算机

实验记录(可分栏或加页)

#include”stdio.h”

typedefstruct{

inta[30];intlength;

}sqtable;

sqtablest;intb=0;

voidcreatest(intk)

{inti;printf("Pleaseinputdata:

”);st。

a[0]=—100;

for(i=1;(!

b&&(i<=k));i++)

{scanf(”%d”,&(st.a[i]));

if(st.a[i]〈st.a[i-1]){printf(”Inputdataerror.\n");b=1;

}}

if(!

b){st。

length=k;

printf(”Thetableisbuilted。

\n”);

}}

intstfind(sqtablest,intl,inth,inty)

{intm;while(l〈=h)

{m=(l+h)/2;if(y==st。

a[m]);returnm;

elseif(y

a[m])returnstfind(st,l,m—1,y);

elsereturnstfind(st,m+1,h,y);}}

main(){intn,x,l,h,s;

l=1;h=st.length;printf(”\nPleaseinputn:

");scanf("%d”,&n);

createst(n);h=st。

length;if(b==0)

{printf(”Pleaseinputyouwantfindvalue:

");

scanf(”%d”,&x);s=stfind(st,l,h,x);}

printf("Find%dinposition%d.\n",x,s);

实验结果:

心得:

本次试验让我明白了查找的重要性,在查找过程中要注意程序的时间复杂度及循环次数。

实验名称

实验五排序

实验目的和要求:

通过本次实验,掌握线性表的排序方法,并分析时间复杂度.

实验内容

设计一个用链表表示的直接选择排序算法,并用程序实现。

算法说明:

已知待排序初始序列用单链表存贮,头指针head指向第一个结点,从这个待排序列中找出最小结点,插入head之后,用r来指示.r以前为已排序序列,r以后为未排序序列。

再从未排序序列中找出最小结点插入r的后面,让r指向这个结点。

反复执行这个过程,直到排好序.

主要仪器设备

台式或笔记本计算机

实验记录(可分栏或加页)

#include〈stdio.h〉

voidSort(int*a,intm){

inti=0;intj=m-1;inttemp;

while(i〈=j){

while(i〈m&&a[i]>=0){i++;}

while(j〉=0&&a[j]〈0){j--;}

if(i〈j){

temp=a[i];a[i]=a[j];a[j]=temp;

i++;j--;

}}}

intmain(){

intn,i;int*p;printf(”你有多少个数据有待整序(必须大于1):

\n");

scanf(”%d”,&n);while(n〈2){

printf("你输入的数值不合理,请重新输入:

\n”);scanf(”%d”,&n);}

p=(int*)malloc(n*sizeof(int));printf("请输入数据:

\n");

for(i=0;i

Sort(p,n);printf(”整序后的数据为:

\n");

for(i=0;i

printf("\n");system(”pause");return0;

}

实验结果:

心得:

排序过程中应注意细节,循环次数,细节决定成败

展开阅读全文
相关资源
猜你喜欢
相关搜索
资源标签

当前位置:首页 > 自然科学 > 物理

copyright@ 2008-2023 冰点文库 网站版权所有

经营许可证编号:鄂ICP备19020893号-2