数据结构实验参考源代码 实验36.docx

上传人:b****7 文档编号:9629613 上传时间:2023-02-05 格式:DOCX 页数:52 大小:37.81KB
下载 相关 举报
数据结构实验参考源代码 实验36.docx_第1页
第1页 / 共52页
数据结构实验参考源代码 实验36.docx_第2页
第2页 / 共52页
数据结构实验参考源代码 实验36.docx_第3页
第3页 / 共52页
数据结构实验参考源代码 实验36.docx_第4页
第4页 / 共52页
数据结构实验参考源代码 实验36.docx_第5页
第5页 / 共52页
点击查看更多>>
下载资源
资源描述

数据结构实验参考源代码 实验36.docx

《数据结构实验参考源代码 实验36.docx》由会员分享,可在线阅读,更多相关《数据结构实验参考源代码 实验36.docx(52页珍藏版)》请在冰豆网上搜索。

数据结构实验参考源代码 实验36.docx

数据结构实验参考源代码实验36

实验三树形结构

以下为部分参考程序:

//递归实现二叉树的3种遍历

#include

typedefcharelemtype;

structbitree

{定义二叉树数据类型

elemtypedata;//结点信息

bitree*lchild,*rchild;//左右孩子

};

bitree*create()//建立二叉链表

{bitree*root,*s,*q[100];//q为队列,最大空间为100

intfront=l,rear=0;//队列头、尾指针

charch;

root=NULL;

cout<<”请输入结点值(‘,’为虚结点,‘#’结束):

”<

cin>>ch;

while(ch!

=’#’)

{s==NULL;

if(ch!

=’,’)

{s=newbitree;

s->data=ch;

s->lchild=NULL;

s->rchild=NULL;

}

rear++;

q[rear]=s;//进队

if(rear==1)root=s;

else

{if((s!

=NULL)&&(q[front]!

=NULL))

{if(rear%2==0)q[front]->lchild=s;

elseq[front]->rchid=s;}

if(rear%2==1)front++;//出队

}

cin>>ch;}

returnroot:

}

voidpreorder(bitree*root)//先序遍历

{bitree*p;

p=root;

if(p!

=NULL)

{cout<data<<””;

preorder(p->lchild);

preorder(p->rchild);

}}

voidinorderl(bitree*root)//中序遍历

{bitree*p;

p=root;

if(p!

=NULL)

{

inorder(p->lchild);

cout<data<<“”;

inorder(p->rchild);

}}

voidpostorder(bitree*root)//后序遍历

{bitree*p;

p=root;

if(p!

=NULL)

{

postorder(p->lchild);

postorder(p->rchild);

cout<data<<“”;

}}

voidmain()

{bitree*root;

root=create();//建立二叉链表

cout<<”先序遍历的结果”<

preorder(root);cout<

cout<<”中序遍历的结果”<

inorder(root);cout<

cout<<”后序遍历的结果”<

postorder(root);cout<

}

//用非递归实现二叉树的3种遍历,部分遍历程序如下:

voidpreorderl(bitree*root)//先序遍历

{bitree*p,*s[100];//s为堆栈

inttop=0;//top为栈顶指针

p=root;

while((p!

=NULL)||(top>0))

{while(p!

=NULL)

{cout<data<<””;

s[++top]=p;//进栈

p=p->lchild;

}

p=s[top--];//退栈

p=p->rchild;

}

}

voidinorderl(bitree*root)//中序遍历

{bitree*p,*s[100];

inttop=0;

p=root;

while((p!

=NULL)Il(top>O))

{while(p!

=NULL)

{s[++top]=p;p=p->lchild;}

{p=s[top--];

cout<data<<“”;

p=p->rchild;

voidpostorderl(bitree*root)//后序遍历

(bitree*p*sl[100];

ints2[100],top=0,b;

p=root;

do

{while(p!

=NULL)

{s1[top]=p;s2[top++]=0;

p=p->lchild;)

if(top>0)

(b=s2[--top];

p=s1[top];

if(b==0)

{sl[top]=p;s2[top++]=l;

p=p->rchild;}

else

{cout<data<<””;

p=NULL;

}}

}while(top>0);

}

//建立二叉链表参考前述程序

//按照层次遍历二叉链表

#include

typedefcharelemtype;

structbitree

{

elemtypedata;//结点信息

bitree*lchild,*rchild;//左右孩子

};

//按层次遍历二叉树(建立二叉链表同前)

voidlorder(bitree*t)

{bitreeq[100],*p;//q代表队列

intf,r;//f,r类似于队列头、尾指针

q[1]=t;f=r=l;

cout<<”按层次遍历二叉树的结果为:

”;

whileif<==r)

{p=q[f];f++;//出队

cout<data<<””;

if(P->lchild!

=NULL)

{r++;q[r]=p->lchild;}//入队

ifp->rchild!

=NULLl.

{r++;q[r]=p->rchild;}//入队

}

cout<

}

voidmain()

{bitree*t;

t=create0;//建立二叉链表

lorder(t);//:

按层次遍历二叉树

}

//将二叉树的左右子树进行交换

voidleftOright(bitree*r)

{bitree*root=r;

if(root!

=NULL)

{leftOright(root->lchild);

leftOright(root->rchild);

bitree*t=root->lchild;

root->lchild=root->rchild;

root->rchild=t;

}}

//主函数

voidmain()

{bitree*root;

root=create();//建立二叉链表

cout<

cout<<”先序遍历的结果”<

preorder(root);cout<

cout<<”中序遍历的结果”<

inorder(root);cout<

cout<<”后序遍历的结果”<

postorder(root);cout<

}

leftTOright(root);

cout<

cout<<”先序遍历的结果”<

preorder(root);cout<

cout<<”中序遍历的结果”<

inorder(root);cout<

cout<<”后序遍历的结果”<

postorder(root);cout<

}

实验四图形结构

以下为部分参考程序:

//邻接矩阵的存储结构

typedefstruct

{intvertex;

}node;

typedefstruct

{intadj;

}arc;

typedefstruct

{nodenode[maxnode];

arcarcs[maxnode][maxnode];

}graph;

//实现插入、删除边的操作

voidins_arc(graph*g,intv,intw)

{g->arcs[v][w].adj=l;

return;

}

voiddel_arc(graph*g,intv,intw)

{g->arc[v][w].adj=0;

retum;

}

//内容1参考程序

#definemaxnode40

#definenull0

#include

typedefstructst_arc

{intadjvex;

intweight;

structst_arc*nextrac;

}arcnode;

typedefstruct

{intvertex;

structst_arc*firstarc;

}vernode;

typedefvernodeadjlist[maxnode];

voiddel_arc(vernodeg[],intv,intw)//删除从顶点v到顶点w的边

{arcnode*rl,*r2;

rl=g[v].frrstarc;

r2=rl;

while(r1!

=null&&r1->adjvex!

=w)

{r2=rl;

rl=rl->nextarc;

}

if(rl==null)

{printf(”noedgev-w.”);

return;

}

else

if(r1==r2)//当只有一个边结点时

g[v].firstarc=r1->nextarc;

else

r2->nextarc=r1->nextarc;//有多个边结点时

rl=g[w].firstarc;

r2=rl;

while(r1!

=null&&r1->adjvex!

=v)//在以v为头结点的链表中,删除相应的

//边结点

{r2=rl;

rl=rl->nextarc;

if(rl==null)

{printf(”noedgev-w.”);

return;

}

else

if(r1==r2)

g[w].firstarc=rl->nextarc;

else

r2->nextarc=r1->nextarc;

}

voidprint(vernodeg[],intn)//打印图中各结点的结构

{arcnode*q;

inti;

printf(”adjacencylistofthegraph:

\n”);

for(i=0;i

{printf(”\t%d\t”,i);

printf(”%d\t”,g[i].vertex);

q=g[i].firstarc;

while(q!

=null)

{printf(”%d\t”,q->adjvex);

printf(”%d\t”,q->weight);

q=q->nextarc;

}

printf(”\n”);

}

}

main()

{inti,j,n,k,w,v;

arcnode*p,*q;

adjlistg;

printf(”Inputnode:

”);//输入图中顶点个数

scanf(”%d”,&n);

for(k=0;k

{printf(”node%d=”,k);

scanf(“%d”,&g[k].vertex);

g[k].firstarc=null;//对顺序存储部分初始化

}

for(;;)//输入各边,并将对应的边结点插入到链表中

{printf(”Insertedgei-j,w:

”)

scanf(”%d”,&i);

scanf(”%d”,&j);

scanf(”%d”,&w);

if(i==-1&&j==-1&&w=-1)

break;

q=(arcnode*)malloc(sizeof(arcnode));

q->adjvex=j;

q->weight=w;

q->nextarc=g[i].firstarc;//头指针指向新的边结点

g[i].firstarc=q;

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

p->nextarc=g[i].firstarc;

g[i].firstarc=q;

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

p->adjvex=i;

p->weight=w;

p->nextarc=g[j].firstarc;

g[j].firstarc=p;

}

print(g,n);

printf(”DeleteedgeV-w:

”);

scanf(”%d%d”,&v,&w);

del_arc(g,v,w);

print(g,n);

}

1内容2的知识要点:

构造有向图链接表与无向图链接表的算法区别是:

无向图两结点无向对偶,因而邻接表有一定的对偶性;有向图两结点间有向无对偶关系,因而建立邻接表时应根据输入的顶点及边的有向关系建立(当箭头方向离开结点为有关,当箭头方向指向结点为无关)。

//内容2的参考程序

//有向图链表的存储结构为:

typedefstructst_arc

{intaajvex;//存放依附于该边的另一顶点在一维数组中的序号

intweight;//存放和该边有关的信息,如权值等

structst_arc*nextarc;//依附于该顶点的下一个边结点的指针

}arcnode;

typedefstruct

{intvertex;//存放与顶点有关的信息

structst_arc*firstarc;

}vernode;//存储顶点信息

typedefvernodeadjlist[maxnode];

//参考程序见内容1

2内容3的知识要点:

深度优先搜索遍历图的算法:

首先访问指定的起始顶点v0,从vo出发,访问vo的一个未被访问过的邻接顶点w1,再从w1出发,访问w1的一个未被访问的顶点w2,然后从w2出发,访问w2的一个未被访问的邻接顶点w3,依此类推,直到一个所有邻接点都被访问过为止。

图采用邻接表作存储结构,图的深度优先遍历次序为

①→②→④→⑤→⑥→③

参考程序运行过程中,深度优先遍历时指针p的移动方向示意如图下所示,图中p1、p2、p3、p4、p5和p6为深度优先遍历图的各结点时,指针p的移动次序。

//内容3的参考程序

#definemaxnode40

#definenull0

#include

typedefstructst_arc//定义结构体

{intadivex;

intweigh;

structst_arc*nextarc;

}arcnode;

typedefstruct

{intvertex;

structst_arc*firstarc;

}vernode;

typedefvernodeadjlist[maxnode];

voidtrave(adjlistg,intn)//采用邻接表作存储结构的/深度优先遍历

{inti,visited[maxnode];//数组visited标志图中的定点是否已被访问

voiddfs();

for(i=0;i

visited[i]=0;//数组初始化

for(i=0;i

if(visited[i]==0)

dfs(g,i,visted);

}

voiddfs(adjlistg,intk,intvisited[])//从顶点k出发,深度优先遍历图g。

{arcnode*p;

intw;

visited[k]=1;

printf(%d,g[k],vertex);

p=g[k],firstarc;

while(p!

=null)

{w=p->adjvex;

if(visited[w]==0)

dfs(g,w,visited);

p=p->nextarc;

}

}

main()

{inti,j,n,k,v;

arcnode*p,*q;

adjlistg;

printf(“Inputnode:

”);

scanf(“%d”,&n);

for(k=0;k

{printf(“node%d=”,k);

g[k].firstarc=null;

}

for(;;)

{printf(“Insertedgei-j:

”);

scanf(“%d”,&i);

scanfi(“%d”,&j);

if(i==-1&&j==-1)

break;

q=(arcnode*)malloc(sizeof(arcnode));

q->adjvex=j;

q->nextarc=g[i].firstarc;

g[i]firstarc=q;

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

p->adjvex=i;

p->nextarc=g[i].firstarc;

g[i]_firstarc=p;}

printf(“dfs:

”);

trave(g,n);//深度优先遍历图。

printf(“\n”);

}

3内容4的知识要点:

广度优先遍历图的算法:

首先访问指定的起始顶点v0,从v0出发,访问v0的所有未被访问的邻接顶点w1,w2,…,wk,然后再依次从w1,w2,…,wk出发,访问所有未被访问过的邻接顶点,依此类推,直到图中所有未被访问过的邻接顶点都被访问过为止。

根据广度优点遍历的规则,在其算法实现中,借助一个队列g-queue来存放已被访问过的顶点。

从指定顶点开始,每访问一个顶点,就将它入队并排在队尾,然后从队头取出一个顶点,访问该顶点的所有未被访问的邻接点,依此类推,直至队列为空且图中结点均被访问过为止。

图的广度优先遍历次序为:

①→②→③→④→⑤→⑥

参考程序运行过程中,广度优先搜索时指针p的移动示意如下图所示,图中片p1、p2、p3、p4、p5和p6为广度优先遍历图的各结点指针p的移动次序。

//内容4的参考程序

#definemaxnode40

#definenull0

#include

typedefstructst_arc//定义结构体

{intadivex;

intweigh;

structst_arc*nextarc;

}arcnode;

typedefstruct

{intvertex;

structst_arc*firstarc;

}vernode;

typedefvernodeadjlist[maxnode];

typedefstruct

{intqueue[maxnode];

intfront;

intrear;

}qqtype;

voidintiate(qqtype*q)//初始化队列

{q->front=-1;

q->rear=-1;

}

intenter(qqtype*q,intx)//将X入队

{if(q->rear==maxnode-1)

return(-1);

else

{q->rear++;

q->queue[q->rear]=x;

return(0);

}

}

intdelete(qqtype*q)//出队

{if(q->front==q->rear)

return(null);

else

{q->front++;

return(q->queue[q->front]);

}

}

intemtype(qqtype*q)

{if(q->front==q->rear)

return(-1);

return(0);

}

voidbfs(adjlistg,intk,intvisited[])//从顶点k出发进行广度优先遍历

{arcnode*p;

intw;

intiate(g);

visited[k]=1;//访问点从k开始,并把它入队。

printf(“%d”,g[p->adjvex].vertex);

enter(g,k);

while(emptye(g)==0)

{w=delete(g);

p=g[w].firstarc;

while(p!

=null)

{if(visited[p->adjves]==0)

{printf(“%d”,g[p->adjvex].verte);

visited[p->adjvex]=1;

enter(g.p->adjvex);

}

p=p->nextarc;

}

}

}

voidtrave(adjlistg,intn)//图采用邻接表存储的广度优先遍历

{inti,visited[maxnode];//数组visited标志图中的定点是否已被访问。

for(i=0;i

visited[i]=0;//数组初始化

for(i=0;i

if(visited[i]==0)

bfs(g,i,visted);

}

main()

{inti,j,n,k,v;

arcnode*p,*q;

adjlistg;

printf(“Inputnode:

”);

scanf(“%d”,&n);

for(k=0;k

{printf(“node%d=”,k);

scanf(“%d”,&g[k].vertex);

g[k].firstarc=null;

}

for(;;)

{printf(“Insertedgei-j:

”);

scanf(“%d”,&i);

scanfi(“%d”,&j);

if(i==-1&&j==-1)

break;

q=(arcnode*)malloc(sizeof(arcnode));

q->adjvex=j;

q->nextarc=g[i].firstarc;

g[i]firstarc=q;

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

p->adjvex=i;

p->nextarc=g[i].firstarc;

g[i]_firstarc=p;

}

printf(“bfs:

”);

trave(g,n);//广度优先遍历图。

printf(“\n”);

}

 

实验五查找

以下为部分参考程序:

//参考程序

#include

#defineKEYTYPE

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

当前位置:首页 > 小学教育 > 其它课程

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

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