数据结构实验报告讲解.docx

上传人:b****0 文档编号:964181 上传时间:2022-10-14 格式:DOCX 页数:31 大小:34.19KB
下载 相关 举报
数据结构实验报告讲解.docx_第1页
第1页 / 共31页
数据结构实验报告讲解.docx_第2页
第2页 / 共31页
数据结构实验报告讲解.docx_第3页
第3页 / 共31页
数据结构实验报告讲解.docx_第4页
第4页 / 共31页
数据结构实验报告讲解.docx_第5页
第5页 / 共31页
点击查看更多>>
下载资源
资源描述

数据结构实验报告讲解.docx

《数据结构实验报告讲解.docx》由会员分享,可在线阅读,更多相关《数据结构实验报告讲解.docx(31页珍藏版)》请在冰豆网上搜索。

数据结构实验报告讲解.docx

数据结构实验报告讲解

浙江师范大学

实验报告

 

学院:

数理与信息工程学院

专业:

计算机科学与技术

姓名:

杨富生

学号:

201531910137

课程名称:

数据结构

指导教师:

钟发荣

实验时间:

2016-06-15

 

2016年6月15日

实验一

1.实验要求

1.1掌握数据结构中线性表的基本概念。

1.2熟练掌握线性表的基本操作:

创建、插入、删除、查找、输出、求长度及合并并运算在顺序存储结构上的实验。

2.实验内容

2.1编写一个函数,从一个给定的顺序表A中删除元素值在x到y之间的所有元素,要求以较高效率来实现。

#include

typedefintelemtype;

#definemaxsize10

intdel(intA[],intn,elemtypex,elemtypey)

{

inti=0,k=0;

while(i

{if(A[i]>=x&&A[i]<=y)

k++;

else

A[i-k]=A[i];

i++;

}

return(n-k);

}

voidmain()

{

inti,j;

inta[maxsize];

printf("输入%d个数:

\n",maxsize);

for(i=0;i

scanf("%d,",&a[i]);

j=del(a,maxsize,1,3);

printf("输出删除后剩下的数:

\n");

for(i=0;i

printf("%d"\n,a[i]);

}

2.2试写一个算法,在无头结点的动态单链表上实现线性表插入操作INSERT(L,i,b)。

voidInsert(Linklist&L,inti,elemtypex)

{

if(!

L)

{

L=(Linklist)malloc(sizeof(Lnode));

(*L).data=x;(*L).next=NULL;

}

else

{

if(i==1)

{

s=(Linklist)malloc(sizeof(Lnode));

s->data=x;s->next=L;L=s;

}

else

{

p=L;j=1;

while(p&&j

{j++;p=p->next;}

if(p||j>i-1)

returnerror;

s=(Linklist)malloc(sizeof(Lnode));

s->data=x;s->next=p->next;p->next=s;

}

}

}

2.3生成两个多项式PA和PB,求他们的和,输出“和多项式”。

typedefstructnode

{intexp;

floatcoef;

structnode*next;

}polynode;

polynode*polyadd(polynode*pa,polynode*pb)

{

polynode*p,*q,*pre,*r;

floatx;

p=pa->next;

q=pb->next;

pre=pa;

while((p!

=NULL)&&(q!

=NULL))

if(p->exp>q->exp)

{

r=q->next;

q->next=p;

pre->next=q;

pre=q;

q=r;

}

else

if(p->exp==q->exp)

{

x=p->coef+q->coef;

if(x!

=0)

{p->coef=x;

s=p;

}

else

{pre->next=p->next;

free(p);

}

p=pre->next;

r=p;

q=q->next;

free(r);

}

else

if(p->expexp)

{

pre=p;

p=p->next;

}

if(q!

=NULL)

pre->next=q;

free(pb);

}

2.4设计一个统计选票的算法,输出每个候选人的得票结果。

typedefintelemtype

typedefstructlinknode

{

elemtypedata;

structlinknode*next;

}nodetype;

nodetype*create()

{

elemtyped;

nodetypeh=NULL,*s,*t;

inti=1;

printf("建立单链表:

\n");

while

(1)

{

printf("输入第%d个结点数据域",i);

scanf("%d",&d);

if(d==0)break;

if(i==1)

{

h=(nodetype*)malloc(sizeof(nodetype));

h->data=d;h->next=NULL;t=h;

}

else

{

s=(nodetype*)malloc(sizeof(nodetype));

s->data=d;s->next=NULL;t->next=s;

t=s;

}

i++;

}

returnh;

}

voidsat(nodetype*h,inta[])

{

nodetype*p=h;

while(p!

=NULL)

{

a[p->data]++;

p=p->next;

}

}

voidmain()

{

inta[N+1],i;

for(i=0;i

a[i]=0;

nodetype*head;

head=create();

sat(head,a);

printf("候选人:

");

for(i=1;i<=N;i++)printf("%3d",i);

printf("\n得票数\n");

for(i=1;i<=N;i++)

printf("%3d",a[i]);

printf("\n");

}

3.实验心得体会

线性表是最简单的、最常用的一种数据结构,是实现其他数据结构的基础。

 

实验二

1.实验要求

1.1了解栈和队列的特性,以便灵活运用。

1.2熟练掌握栈和有关队列的各种操作和应用。

2.实验内容

2.1设一个算术表达式包括圆括号,方括号和花括号三种括号,编写一个算法判断其中的括号是否匹配。

#include

#include

#include

#defineNULL0

typedefstructlist

{

charstr;

structlist*next;

}list;

voidpush(char,list*);

intpop(char.list*);

voiddeal(char*str);

main(void)

{charstr[20];

printf("\n请输入一个算式:

\n");

gets(str);

deal(str);

printf("正确!

");

getchar();

return0;

}

voiddeal(char*str)

{list*L;

L=(list*)malloc(sizeof(list));

if(!

L)

{

printf("错误!

");

exit(-2);

}

L->next=NULL;

while(*str)

{

if(*str=='('||*str=='['||*str=='{')

push(*str,L);

else

if(*str==')'||*str==']'||*str=='}')

if(pop(*str,L))

{puts("错误,请检查!

");

puts("按回车键退出");

getchar();exit(-2);

}

str++;

}

if(L->next)

{puts("错误,请检查!

");

puts("按任意键退出");

getchar();exit(-2);

}

}

voidpush(charc,list*L)

{list*p;

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

if(!

p)

{

printf("错误!

");

exit(-2);

}

p->str=c;

p->next=L->next;

L->next=p;

}

#definecheck(s)if(L->next->str==s){p=l->next;L->next=p->next;free(p);return(0);}

intpop(charc,list*L)

{

list*p;

if(L->next==NULL)return1;

switch(c)

{

case')':

check('(')break;

case']':

check('[')break;

case'}':

check('{')break;

}

return1;

 

实验三

1.实验要求

1.1掌握二叉树,二叉树排序数的概念和存储方法。

1.2掌握二叉树的遍历算法。

1.3熟练掌握编写实现树的各种运算的算法。

2.实验内容

2.1编写程序,求二叉树的结点数和叶子数。

#include

#include

structnode{

chardata;

structnode*lchild,*rchild;

}bnode;

typedefstructnode*blink;

blinkcreat()

{

blinkbt;

charch;

ch=getchar();

if(ch=='')return(NULL);

else

{

bt=(structnode*)malloc(sizeof(bnode));

bt->data=ch;

bt->lchild=creat();

bt->rchild=creat();

}

returnbt;

}

intn=0,n1=0;

voidpreorder(blinkbt)

{

if(bt)

{

n++;

if(bt->lchild==NULL&&bt->rchild==NULL)

n1++;

preorder(bt->lchild);

preorder(bt->rchild);

}

}

voidmain()

{

blinkroot;

root=creat();

preorder(root);

printf("此二叉数的接点数有:

%d\n",n);

printf("此二叉数的叶子数有:

%d\n",n1);

}

2.2编写递归算法,求二叉树中以元素值为X的结点为

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

当前位置:首页 > PPT模板 > 可爱清新

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

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