数据结构实验指导书.docx

上传人:b****6 文档编号:9012572 上传时间:2023-02-02 格式:DOCX 页数:17 大小:20.22KB
下载 相关 举报
数据结构实验指导书.docx_第1页
第1页 / 共17页
数据结构实验指导书.docx_第2页
第2页 / 共17页
数据结构实验指导书.docx_第3页
第3页 / 共17页
数据结构实验指导书.docx_第4页
第4页 / 共17页
数据结构实验指导书.docx_第5页
第5页 / 共17页
点击查看更多>>
下载资源
资源描述

数据结构实验指导书.docx

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

数据结构实验指导书.docx

数据结构实验指导书

实验一线性表的链式存储

实验目的

掌握单链表的结构特性和基本操作算法,利用指针来实现单链表的建立、输出、插入和删除。

实验内容

设计算法,删除非递减的单链表(允许出现值域重复的结点)中值域重复的结点,并分析算法的时间复杂度。

(写出算法思想及代码)

程序代码:

#include

#include

typedefintelemtype;

typedefstructnode

{elemtypedata;

structnode*next;

}linklist;

linklist*initlist(linklist*head)

{head=(linklist*)malloc(sizeof(linklist));

head->next=NULL;

returnhead;

}

intlistlength(linklist*head)

{inti=0;

linklist*p;

p=head->next;

while(p!

=NULL)

{i++;

p=p->next;}

returni;

}

intlistempty(linklist*head)

{return(head->next==NULL);}

voiddestroylist(linklist*head)

{linklist*p,*q;

p=head;

q=p->next;

while(q!

=NULL)

{free(p);

p=q;

q=p->next;

}

free(p);

}

intgetelem(linklist*head,inti)

{intj=0;

linklist*p=head;

while((p->next!

=NULL)&&(j

{p=p->next;

j++;

}

if(i==j){printf("%d\n",p->data);return1;}

elsereturn0;

}

intlocatelem(linklist*head,inte)

{linklist*p=head->next;

intn=1;

while(p!

=NULL&&p->data!

=e)

{p=p->next;

n++;

}

if(p==NULL)return0;

elsereturnn;

}

intlistinsert(linklist*head,inti,intx)

{linklist*s,*p=head;

intj=0;

while(j

=NULL)

{j++;

p=p->next;}

if(p==NULL)return0;

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

s->data=x;

s->next=p->next;

p->next=s;

return1;}

intlistdelete(linklist*head,inti)

{

linklist*q,*p=head;

intj=0;

while(jnext!

=NULL)

{j++;

p=p->next;}

if(p->next==NULL)return0;

q=p->next;

if(q==NULL)return0;

p->next=q->next;

free(q);

return1;

}

voiddisplist(linklist*head)

{

linklist*p=head->next;

while(p!

=NULL)

{printf("%d",p->data);

p=p->next;}

printf("\n");

}

voidmain()

{

linklist*head;

elemtypee;

head=initlist(head);

listinsert(head,1,1);listinsert(head,2,2);

listinsert(head,3,3);listinsert(head,4,4);

listinsert(head,5,5);

printf("length=%d\n",listlength(head));

getelem(head,3);

printf("itisthe%d",locatelem(head,4));

listinsert(head,2,98);

displist(head);

listdelete(head,3);

displist(head);

getch();

}

实验二简单运算器的实现

实验目的

编写程序实现简单的四则算术运算求值,掌握栈的结构特点和操作特性,利用出栈和进栈解决四则运算中运算符的优先级问题。

实验内容

设计算法,用栈来实现表达式的值:

A-B*C/D+E^F(各变量使用键盘输入)。

#include

#include

typedefstruct

{floats[10];

inttop;

}stack;

voidinistack(stack*st)

{

st->top=-1;

}

floatgettop(stack*st)

{floatz;

z=st->s[st->top];

return(z);

}

voidpush(stack*st,floatx)

{st->top=st->top+1;

st->s[st->top]=x;

}

floatpop(stack*st)

{floatz;

z=st->s[st->top];

st->top--;

return(z);

}

floatoperate(floata,chartheta,floatb)

{floatz;

switch(theta)

{case'+':

z=a+b;break;

case'-':

z=a-b;break;

case'*':

z=a*b;break;

case'/':

z=a/b;break;

}

return(z);

}

 

charprecede(chara,charb)

{charz;

if((b=='+')||(b=='-')||(b=='*')||(b=='/')||(b=='(')||(b==')')||(b=='='))

switch(a)

{case'+':

case'-':

if((b=='*')||(b=='/')||(b=='('))z='<';

elsez='>';break;

case'*':

case'/':

if(b=='(')z='<';

elsez='>';break;

case'(':

if(b=='=')z='e';

elseif(b==')')z='=';

elsez='<';

break;

case')':

if(b=='(')z='e';

elsez='>';break;

case'=':

if(b=='=')z='=';

elseif(b==')')z='e';

elsez='<';break;

}

elsez='e';

return(z);

}

 

voidaa()

{printf("***********************\n");

printf("\n");

printf("FormulaCaculation\n");

printf("\n\n");

printf("***********************\n");

}

 

intin(charch,charopn[])

{inti;

for(i=0;i<7;i++)

if(ch==opn[i])return1;

if(i==7)return0;

}

 

main()

{stack*optr,*opnd;

charw;

charopn[7]={'+','-','*','/','(',')','='};

floattheta,a,b;

clrscr();

optr=(stack*)malloc(sizeof(stack));

opnd=(stack*)malloc(sizeof(stack));

inistack(opnd);

push(optr,(float)'=');

aa();

printf("Inputthecaculationformula:

");

scanf("%c",&w);

while((w!

='=')||((char)gettop(optr)!

='='))

{if(!

in(w,opn))

{w=w-'0';push(opnd,(float)w);

scanf("%c",&w);

}

elseswitch(precede((char)gettop(optr),w))

{case'<':

push(optr,(float)w);scanf("%c",&w);break;

case'=':

pop(optr),scanf("%c",&w);break;

case'>':

theta=pop(optr);b=pop(opnd);

a=pop(opnd);

push(opnd,operate(a,(char)theta,b));break;

case'e':

printf("Error!

!

!

");

gotoend;

}

}

printf("Theansweris:

%f",gettop(opnd));

end:

free(opnd);

free(optr);

getch();

}

实验三哈夫曼编码的构造

一、实验目的

1.理解和掌握树型结构的特点和基本操作;

2.利用数组存储哈夫曼编码并定义所需的属性结构;

3.掌握哈夫曼树的结构特点和哈夫曼编码的构造算法及应用

二、实验内容

理解哈夫曼树的结构特定,利用哈夫曼树的的构造算法进行哈夫曼编码的构造。

题目如例6-2

(1)定义程序所需各种数据类型;

(2)仔细理解和分析哈夫曼编码构造算法,编写程序;

(3)调试并修改程序,得到正确的实验结果。

#include"stdio.h"

#defineN100

typedefstruct

{charch;

intweight;

intlchild,rchild,parent;

}HTNODE;/*定义Huffman树的结点类型*/

typedefstruct

{char*code;

charleaf;

}CODE;/*定义字符编码类型*/

voidhufcoding(intn)

{HTNODEhuftree[N];/*定义hufftree数组存放Huffman树*/

intw[N];/*定义一个权值数组,按权值大小的升序排列*/

CODEcd[N];/*定义cd数组,专门存放字符的值和对应编码*/

inti,j,k,s1,s2,s,m,f,c,sum=0;

chartemp[N];charch;

printf("pleaseinputtheweightofleaf(smalltolarge):

\n");

for(i=1;i<=n;i++)scanf("%d",&w[i]);/*按升序输入权值存放在数组w中*/

ch=getchar();

m=2*n-1;/*Huffman树中的结点总数*/

printf("pleaseinputthesecharcters:

\n");/*输入要进行编码的字符*/

for(i=1;i<=n;i++)/*建立Huffman树的初始状态即hufftree数组的初始状态*/

{huftree[i].weight=w[i];

huftree[i].lchild=0;

huftree[i].rchild=0;

huftree[i].parent=0;

scanf("%c",&huftree[i].ch);

}

for(;i<=m;i++)/*从n+1开始,存放内结点,此时为内结点的初始状态*/

{huftree[i].weight=0;

huftree[i].lchild=0;

huftree[i].rchild=0;

huftree[i].parent=0;

}

k=0;/*用k表示建立的内点的个数,开始为0*/

for(i=n+1;i<=m;i++)

{k++;s1=2*k-1;s2=s1+1;/*每次建立内点所选出的结点的下标值s1和s2*/

sum=huftree[s1].weight+huftree[s2].weight;/*内点的权值用sum表示*/

j=i-1;

while(j>=s2+1&&sum

{huftree[j+1]=huftree[j];j--;}/*从后往前比较,按升序将内点插入到数组中*/

huftree[j+1].weight=sum;

huftree[s1].parent=j+1;/*将产生的内点与s1和s2结点的关系进行修正*/

huftree[s2].parent=j+1;

huftree[j+1].lchild=s1;

huftree[j+1].rchild=s2;

}/*Huffman树建立完成,存放在huftree数组中*/

/*******************************************************************/

s=0;/*叶结点的个数用s表示,开始为0*/

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

{c=0;

if(huftree[i].lchild==0&&huftree[i].rchild==0)/*判断是否为叶结点*/

{j=i;/*从叶开始沿着父结点向上回溯,直到根结点*/

for(k=j,f=huftree[j].parent;f!

=0;k=f,f=huftree[f].parent)

if(huftree[f].lchild==k){temp[c]='0';c++;}/*如果是左孩子,得'0'*/

else{temp[c]='1';c++;}/*如果是右孩子,得'1'*/

s++;

cd[s].leaf=huftree[i].ch;/*将叶结点得值赋给编码数组'*/

cd[s].code=(char*)calloc(c,sizeof(char));/*根据得到得编码长度分配空间*/

c--;k=0;/*将得到的反序编码倒序后存放在cd.code指向的空间中*/

while(c>=0)

{cd[s].code[k]=temp[c];k++;c--;}

cd[s].code[k]='\0';

printf("%c,%s\n",cd[s].leaf,cd[s].code);/*得到字符和字符相应的编码'*/

if(s==n+1)break;

}

}

}

/******************************************************************/

main()

{intnum;

printf("pleaseinputthenumberofcharacter:

\n");/*输入要进行编码的字符个数*/

scanf("%d",&num);

hufcoding(num);

}

实验四拓扑排序

【实验目的和要求】

1.理解和掌握图的基本概念,掌握图的两种存储结构邻接表和邻接矩阵;

2.掌握图的相关的定义和在实际应用中的不同特点;

3.通过构造邻接表,实现拓扑排序,并按邻接表形式输出结果。

【实验内容】

用邻接表构造并存储图,并利用拓扑排序算法进行图中结点的拓扑排序。

(1)定义所需的各数据类型以实现图或网的构造,并用用邻接表存储;

(2)按邻接表形式输出图;

(3)利用拓扑排序算法实现网结点的拓扑序列的输出。

#defineNULL0

#defineN50

#include"stdlib.h"

#include"malloc.h"

typedefstructarcnode

{intadjvex;

structarcnode*nextarc;

intweight;

}ARCNODE,*ARCNODEPTR;

typedefstructvnode

{intdata;

ARCNODE*firstarc;

}VNODE,ADJLIST[N];

typedefstruct

{ADJLISTvertices;

intvexnum,arcnum;

intnetwork;

intdigraph;

}ALGRAPH;

#defineLENsizeof(ARCNODE)

voidcreatgraph(ALGRAPH*g)/*建立以邻接表存储的图*/

{inti,j,k,weight;

ARCNODEPTRp;

printf("pleaseinputvexnumandarcnuminthegraph:

\n");

scanf("%d%d",&(*g).vexnum,&(*g).arcnum);

for(i=0;i<(*g).vexnum;i++)

{(*g).vertices[i].data=i;

(*g).vertices[i].firstarc=NULL;

}

printf("SelectNetofGraph?

\n");

printf("Net:

1,Graph:

0,pleaseinput1or0:

\n");

scanf("%d",&(*g).network);

printf("Selectdirectionorno-direction?

\n");

printf("direction:

1,no-direction:

0,pleaseinput1or0:

\n");

scanf("%d",&(*g).digraph);

for(k=0;k<(*g).arcnum;k++)/*输入图中边或弧关联的顶点及权值*/

{if(!

((*g).network))

{printf("pleaseinputtwovexofaedageoraarc:

\n");

scanf("%d%d",&i,&j);}

else

{printf("pleaseinputtwovexandweight:

\n");

scanf("%d%d%d",&i,&j,&weight);}

p=(ARCNODEPTR)malloc(LEN);/*建立邻接表的表头结点*/

p->adjvex=j;

p->nextarc=(*g).vertices[i].firstarc;

(*g).vertices[i].firstarc=p;

if(!

((*g).network))p->weight=0;

elsep->weight=weight;

if(!

((*g).digraph))/*从后往前建立邻接表中的邻接链表*/

{p=(ARCNODEPTR)malloc(LEN);

p->adjvex=i;

p->nextarc=(*g).vertices[j].firstarc;

(*g).vertices[j].firstarc=p;

if(!

((*g).network))p->weight=0;

elsep->weight=weight;

}

}

}

voidprintAL(ALGRAPH*g)/*以邻接表的形式输出所建立的图*/

{inti;ARCNODE*p;

printf("\nthegraphis:

\n");

for(i=0;i<(*g).vexnum;i++)

{printf("%d:

---",i);

p=(*g).vertices[i].firstarc;

while(p)

{printf("%d",p->adjvex);

p=p->nextarc;

if(p)printf("--->");

}

printf("\n");

}

}

voidtopsort(ALGRAPHg)/*利用栈s实现拓扑排序*/

{ints[N],indegree[N];

inti,j,k,n,top=0,count=0;

ARCNODE*p;

n=g.vexnum;

for(i=0;i

{s[i]=0;indegree[i]=0;}

for(i=0;i

{p=g.vertices[i].firstarc;

while(p)

{k=p->adjvex;

indegree[k]++;

p=p->nextarc;

}

}

for(i=0;i

{if(!

indegree[i])

{s[top]=i;top++;}

}

top=top-1;

while(top>=0)/*栈非空时将栈顶顶点出栈输出,并将以它为弧尾的弧删去*/

{k=s[top];top--;

printf("%3d",k);

count++;/*记录下所输出的顶点个数*/

p=g.vertices[k].firstarc;

while(p)/*将邻接自当前输出顶点的顶点的入度减1,如果入度变为0则入栈s*/

{j=p->adjvex;

indegree[j]--;

if(!

indegree[j])s[++top]=j;

p=p->nextarc;

}

}

if(count

!

\n");/*如果输出的顶点个数少于顶点总数,则拓扑排序失败*/

}

main()

{ALGRAPHgra;

creatgraph(&gra);

printAL(&gra);

printf("Thetopsor

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

当前位置:首页 > 高等教育 > 农学

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

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