实验一线性表docx.docx

上传人:b****8 文档编号:28140176 上传时间:2023-07-08 格式:DOCX 页数:37 大小:256.77KB
下载 相关 举报
实验一线性表docx.docx_第1页
第1页 / 共37页
实验一线性表docx.docx_第2页
第2页 / 共37页
实验一线性表docx.docx_第3页
第3页 / 共37页
实验一线性表docx.docx_第4页
第4页 / 共37页
实验一线性表docx.docx_第5页
第5页 / 共37页
点击查看更多>>
下载资源
资源描述

实验一线性表docx.docx

《实验一线性表docx.docx》由会员分享,可在线阅读,更多相关《实验一线性表docx.docx(37页珍藏版)》请在冰豆网上搜索。

实验一线性表docx.docx

实验一线性表docx

 

本科实验报告

 

课程名称:

数据结构

实验项目:

线性结构、树形结构、图结构、查找、排序

实验地点:

逸夫楼502

专业班级:

软件1223班学号:

2012005646

学生姓名:

王萌

指导教师:

杨崇艳

 

2013年11月20日

实验一线性表

一.目的与要求

1.实习的主要目的:

 

2.要求:

 

二.例题

问题描述:

 

输入:

输出:

 

存储结构:

 

算法的基本思想:

 

程序:

#defineNULL0

typedefstructnode{

chara;

structnode*link;

}node,*nodelink;

voidreadlink(nodelinkhead){

nodelinkp,q;

charc;

p=head;

printf("Inputalinktable(astring):

");

scanf("%c",&c);

if(c=='\n')printf("Thisstringisempty。

");

while(c!

='\n'){

q=(nodelink)malloc(sizeof(node));

q->a=c;

p->link=q;

p=q;

scanf("%c",&c);

}

p->link=NULL;

}

voidwritelink(nodelinkhead){

nodelinkq;

if(head->link==NULL)printf("Thislinkisempty。

\n");

for(q=head->link;q;q=q->link)

printf("%c",q->a);

printf("\n");

}

intinsert(nodelinkhead,chark1,chark2){

nodelinkp,q;

p=head->link;

while(p->a!

=k1&&p)

p=p->link;

if(p){

q=(nodelink)malloc(sizeof(node));

q->a=k2;

q->link=p->link;

p->link=q;

return1;

}

else{

printf("Thereisno%c\n",k1);

return0;

}

}

intdelete(nodelinkhead,chark){

nodelinkp,q;

q=head;

p=head->link;

while(((p->a)!

=k)&&p){

q=q->link;

p=p->link;

}

if(p){

q->link=p->link;

return1;

}

else{

printf("Thereisno%c\n",k);

return0;

}

}

voidopside(nodelinkhead){

nodelinkp,q;

p=head->link;

while(p->link){

q=p->link;

p->link=q->link;

q->link=head->link;

head->link=q;

}

}

main()

{

chark1,k2,k3;

nodelinkhead;

head=(nodelink)malloc(sizeof(node));

head->link=NULL;

readlink(head);

if(head->link!

=NULL){

printf("Buildlinkis:

");

writelink(head);}

if(head->link!

=NULL){

printf("Pleaseinputacharyouwanttoinsertafter:

");

k1=getch();

printf("%c\n",k1);

printf("Pleaseinputacharyouwanttoinsert:

");

k2=getch();

printf("%c\n",k2);

if(insert(head,k1,k2)){

printf("After%cinsert%c,linkis:

",k1,k2);

writelink(head);

}

printf("Pleaseinputacharyouwanttodelete:

");

k3=getch();

printf("%c\n",k3);

if(delete(head,k3))

{printf("afterdelete%c,linkis:

",k3);

writelink(head);

}

if(head->link!

=NULL){

printf("Opsiteresultis:

");

opside(head);

writelink(head);

free(head);

}

}

}

调试结果:

程序的优缺点、时空性能以及改进思想,写出心得体会。

 

三.实习题

问题描述:

输入:

输出:

存储结构:

算法的基本思想:

程序:

#include"stdio.h"

#defineMAXSIZE100

#defineRIGHT1

#defineERROR0

typedefintElemType;

typedefstruct{

ElemTypeelem[MAXSIZE];

intlast;

}SeqList;

voidInitlist(SeqList*L){

L->last=-1;

}

voidputseqList(SeqList*L,intn){

inti;

for(i=0;i

scanf("%d",&(L->elem[i]));

L->last=L->last+n;

}

intLenList(SeqList*L){

intLen;

Len=L->last+1;

returnLen;

}

intPositionList(SeqList*L,intX){

intj;

for(j=0;j<=L->last;j++)

if(Xelem[j])

returnj+1;

return(L->last+1);

}

intInsList(SeqList*L,inti,inte){

intk;

if((i<1)||(i>L->last+2)){

printf("thepositioniswrong");

return(ERROR);

}

if(L->last>=MAXSIZE-1){

printf("thelistisfull");

return(ERROR);

}

for(k=L->last;k>=i-1;k--){

L->elem[k+1]=L->elem[k];

}

L->elem[i-1]=e;

L->last++;

return(RIGHT);

}

intOutputSeqList(SeqList*L){

inti;

for(i=0;i<=L->last;i++)

printf("%d,",L->elem[i]);

return(L->elem[i]);

}

voidmain(){

ints,c;SeqListL;

Initlist(&L);

printf("pleaseinputthelength:

");

scanf("%d",&s);

printf("pleaseinputthelist:

");

putseqList(&L,s);LenList(&L);

printf("Pleaseinputanelementtoinsert:

");

scanf("%d",&c);

InsList(&L,PositionList(&L,c),c);

OutputSeqList(&L);

printf("\n");

getch();

}

调试结果:

程序的优缺点、时空性能以及改进思想,写出心得体会。

 

实验二树

一.目的与要求

1.实习的主要目的:

2.要求:

二.例题

问题描述:

 

输入:

一棵二叉树的结点若无子树,则可将其子树看作“.”,输入时,按照前序序列的顺序输入该结点的内容。

对下图,其输入序列为ABD..EH...CF.I..G..。

A

BC

DEFG

HI

输出:

 

存储结构:

算法的基本思想:

程序:

#include

#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;

}

voidrun(NODE*t){

if(t){

run(t->llink);

run(t->rlink);

printf("%c",t->info);

}

}

main()

{

NODE*T;

printf("PLeaseinputatree:

\n");

T=creat();

printf("\n");

if(!

T)

printf("Thisisaemptybinarytree.");

else

{printf("Theresultofposttraveseis:

\n");

run(T);

}

printf("\n");

}

调试结果:

程序的优缺点、时空性能以及改进思想,写出心得体会。

 

三.实习题

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

问题描述:

输入:

一棵二叉树的结点若无子树,则可将其子树看作“.”,输入时,按照前序序列的顺序输入该结点的内容。

对下图,其输入序列为ABD..EH...CF.I..G..。

                       A 

 B          C 

                        

D   E    F      G     

                            H          I 

输出:

存储结构:

 

算法的基本思想:

 

程序:

#include

structBiTree{

chardata;

structBiTree*lchild;

structBiTree*rchild;

};

structBiTree*CreatBiTree(){

charx;

structBiTree*p;

scanf("%c",&x);

if(x!

='.'){

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

p->data=x;

p->lchild=CreatBiTree();

p->rchild=CreatBiTree();

}

else

p=NULL;

returnp;

}

intLeafNum(structBiTree*T){

if(!

T)

return0;

elseif(!

T->lchild&&!

T->rchild)

return1;

else

returnLeafNum(T->lchild)+LeafNum(T->rchild);

}

intmain(){

intnum;

structBiTree*T;

printf("Pleaseinputthetree(pre):

¥n");

T=CreatBiTree();

while(T==NULL){

printf("empoty,again:

¥n");

T=CreatBiTree();

}

num=LeafNum(T);

printf("¥nthesumofleafis:

%d¥n",num);

getch();

}

调试结果:

程序的优缺点、时空性能以及改进思想,写出心得体会。

 

实验三图

一.目的与要求

二.例题

问题描述:

 

输入:

输出:

存储结构:

算法的基本思想:

 

程序:

#include

intnumber;

typedefstruct{

intq[20];

intf,r;

}queue;

intnodelist[20][20];

queueQ;

intz[20];

inta,b,n,i,j,x,y;

intfinished;

voidenq(queue*Q,intx){

Q->q[Q->r]=x;

if(Q->r==19)

Q->r=0;

else

Q->r++;

if(Q->r==Q->f)

printf("Overflow!

\n");

}

front(queue*Q){

if(Q->r==Q->f)

printf("Underflow!

\n");

else

return(Q->q[Q->f]);

}

voiddeq(queue*Q){

if(Q->r==Q->f)

printf("Underflow!

\n");

else{

if(Q->f==19)

Q->f=0;

else

Q->f++;

}

}

intqempty(queueQ){

if(Q.f==Q.r)

return1;

else

return0;

}

voidreadgraph(){

printf("\nPleaseinputn:

");

scanf("%d",&n);

printf("Pleaseinputnodelist[i][j]:

\n");

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

for(j=1;j<=n;j++)

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

}

printf("\n");

printf("List-linkisbulit\n");

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

for(j=1;j<=n;j++)

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

printf("\n");

}

}

voidshortest(inta,intb){

if(a==b)

nodelist[a][a]=2;

else{

enq(&Q,a);

nodelist[a][a]=2;

finished=0;

while(!

qempty(Q)&&!

finished){

a=front(&Q);

deq(&Q);

j=1;

while((j<=n)&&!

finished){

if((nodelist[a][j]==1)&&(nodelist[j][j]!

=2)){

enq(&Q,j);

nodelist[j][j]=2;

z[j]=a;

if(j==b)/*&&(nodelist[a][j]==1))*/

finished=1;

}

if(!

finished)

j++;

}

}

if(!

finished)printf("Thereisnopath.");

}

}

voidwritepath(inta,intb){

i=b;

while(i!

=a){

printf("%d<-",i);

i=z[i];

}

printf("%d",a);

}

main()

{

readgraph();

printf("Pleaseinputa:

");

scanf("%d",&a);

printf("Pleaseinputb:

");

scanf("%d",&b);

Q.f=0;Q.r=0;

shortest(a,b);

if(finished)

writepath(a,b);

}

调试结果:

程序的优缺点、时空性能以及改进思想,写出心得体会。

实验四查找

一.目的与要求

二.例题

问题描述:

输入:

输出:

 

存储结构:

算法的基本思想:

 

程序:

#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]

printf("Inputdataerror.\n");

b=1;

}

}

if(!

b){

st.length=k;

printf("Thetableisbuilted.\n");

}

}

voidstfind(sqtablest,inty){

intf,l,h,m;

l=1;h=st.length;

f=1;

while((l<=h)&&f){

m=(l+h)/2;

if(y==st.a[m])f=0;

elseif(y

elsel=m+1;

}

if(!

f)printf("Find%dinposition%d.\n",y,m);

elseprintf("Notfind%d.\n",y);

}

main(){

intn,x;

printf("\nPleaseinputn:

");

scanf("%d",&n);

createst(n);

if(b==0){

printf("Pleaseinputyouwantfindvalue:

");

scanf("%d",&x);

stfind(st,x);

}

}

调试结果:

程序的优缺点、时空性能以及改进思想,写出心得体会。

 

三.实习题

1.

问题描述:

 

输入:

输出:

存储结构:

算法的基本思想:

 

程序:

#include

#include

#include

structBiTree{

intdata;

structBiTree*lchild;

structBiTree*rchild;

};

structBiTree*CreatBiTree(){

intx;

structBiTree*p;

scanf("%d",&x);

if(x!

=0){

p=(structBiTree*)malloc

(sizeof(structBiTree));

p->data=x;

p->lchild=CreatBiTree();

p->rchild=CreatBiTree();

}

else

p=NULL;

returnp;

}

voidSearchBST(structBiTree*T,intk){

if(!

T){

printf("你查找的数据不存在!

\n");

}

elseif(T->data==k){

printf("你查找的数据存在!

\n");

}

elseif(kdata){

SearchBST(T->lchild,k);

}

else{

SearchBST(T->rchild,k);

}

}

intmain(){

intkey;

chartemp;

structBiTree*T;

printf("请按先序序列输入排序二叉树\n");

T=CreatBiTree();

while(T==NULL){

printf("你输入的对叉树为空,请重新输入:

\n");

temp=getchar();

T=CreatBiTree();

}

printf("请输入你要查找的数据:

\n");

scanf("%d",&key);

SearchBST(T,key);

system("pause");

return0;

}

调试结果:

程序的优缺点、时空性能以及改进思想,写出心得体会。

 

2.

问题描述:

 

输入:

输出:

存储结构:

算法的基本思想:

 

程序:

#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

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

当前位置:首页 > PPT模板 > 动物植物

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

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