数据结构实验参考源代码 实验36Word文档下载推荐.docx
《数据结构实验参考源代码 实验36Word文档下载推荐.docx》由会员分享,可在线阅读,更多相关《数据结构实验参考源代码 实验36Word文档下载推荐.docx(52页珍藏版)》请在冰豆网上搜索。
&
(q[front]!
=NULL))
{if(rear%2==0)q[front]->
lchild=s;
elseq[front]->
rchid=s;
}
if(rear%2==1)front++;
//出队
}
returnroot:
voidpreorder(bitree*root)//先序遍历
{bitree*p;
p=root;
if(p!
=NULL)
{cout<
p->
data<
””;
preorder(p->
lchild);
rchild);
}}
voidinorderl(bitree*root)//中序遍历
{bitree*p;
{
inorder(p->
cout<
“”;
voidpostorder(bitree*root)//后序遍历
{bitree*p;
postorder(p->
“”;
voidmain()
{bitree*root;
root=create();
//建立二叉链表
”先序遍历的结果”<
preorder(root);
”中序遍历的结果”<
inorder(root);
endl:
”后序遍历的结果”<
postorder(root);
//用非递归实现二叉树的3种遍历,部分遍历程序如下:
voidpreorderl(bitree*root)//先序遍历
{bitree*p,*s[100];
//s为堆栈
inttop=0;
//top为栈顶指针
while((p!
=NULL)||(top>
0))
{while(p!
{cout<
””;
s[++top]=p;
//进栈
p=p->
lchild;
p=s[top--];
//退栈
p=p->
rchild;
voidinorderl(bitree*root)//中序遍历
{bitree*p,*s[100];
inttop=0;
p=root;
=NULL)Il(top>
O))
{s[++top]=p;
{p=s[top--];
p=p->
}
}
voidpostorderl(bitree*root)//后序遍历
(bitree*p*sl[100];
ints2[100],top=0,b;
do
{while(p!
{s1[top]=p;
s2[top++]=0;
)
if(top>
0)
(b=s2[--top];
p=s1[top];
if(b==0)
{sl[top]=p;
s2[top++]=l;
else
””;
p=NULL;
}}
}while(top>
0);
//建立二叉链表参考前述程序
//按照层次遍历二叉链表
{
//按层次遍历二叉树(建立二叉链表同前)
voidlorder(bitree*t)
{bitreeq[100],*p;
//q代表队列
intf,r;
//f,r类似于队列头、尾指针
q[1]=t;
f=r=l;
”按层次遍历二叉树的结果为:
”;
whileif<
==r)
{p=q[f];
f++;
if(P->
lchild!
{r++;
q[r]=p->
}//入队
ifp->
rchild!
=NULLl.
q[r]=p->
rchild;
voidmain()
{bitree*t;
t=create0;
lorder(t);
//:
按层次遍历二叉树
//将二叉树的左右子树进行交换
voidleftOright(bitree*r)
{bitree*root=r;
if(root!
{leftOright(root->
lchild);
leftOright(root->
rchild);
bitree*t=root->
lchild;
root->
lchild=root->
rchild=t;
//主函数
endl<
”左右子树交换前的遍历结果”<
leftTOright(root);
”左右子树交换后的遍历结果”<
preorder(root);
实验四图形结构
//邻接矩阵的存储结构
typedefstruct
{intvertex;
}node;
{intadj;
}arc;
{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)
arc[v][w].adj=0;
retum;
//内容1参考程序
#definemaxnode40
#definenull0
#include<
stdio.h>
typedefstructst_arc
{intadjvex;
intweight;
structst_arc*nextrac;
}arcnode;
typedefstruct
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;
if(r1==r2)//当只有一个边结点时
g[v].firstarc=r1->
r2->
nextarc=r1->
//有多个边结点时
rl=g[w].firstarc;
=v)//在以v为头结点的链表中,删除相应的
//边结点
{r2=rl;
rl=rl->
if(rl==null)
{printf(”noedgev-w.”);
if(r1==r2)
g[w].firstarc=rl->
r2->
voidprint(vernodeg[],intn)//打印图中各结点的结构
{arcnode*q;
inti;
printf(”adjacencylistofthegraph:
\n”);
for(i=0;
i<
n;
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->
printf(”\n”);
main()
{inti,j,n,k,w,v;
arcnode*p,*q;
adjlistg;
printf(”Inputnode:
”);
//输入图中顶点个数
scanf(”%d”,&
n);
for(k=0;
k<
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;
weight=w;
nextarc=g[i].firstarc;
//头指针指向新的边结点
g[i].firstarc=q;
p=(arcnode*)malloc(sizeof(arcnode));
p->
adjvex=i;
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;
//依附于该顶点的下一个边结点的指针
{intvertex;
//存放与顶点有关的信息
structst_arc*firstarc;
//存储顶点信息
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
sddio.h>
typedefstructst_arc//定义结构体
{intadivex;
intweigh;
structst_arc*nextarc;
}arcnode;
structst_arc*firstarc;
voidtrave(adjlistg,intn)//采用邻接表作存储结构的/深度优先遍历
{inti,visited[maxnode];
//数组visited标志图中的定点是否已被访问
voiddfs();
visited[i]=0;
//数组初始化
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!
{w=p->
adjvex;
if(visited[w]==0)
dfs(g,w,visited);
{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);
j==-1)
g[i]firstarc=q;
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的参考程序
{intqueue[maxnode];
intfront;
intrear;
}qqtype;
voidintiate(qqtype*q)//初始化队列
{q->
front=-1;
rear=-1;
intenter(qqtype*q,intx)//将X入队
{if(q->
rear==maxnode-1)
return(-1);
{q->
rear++;
q->
queue[q->
rear]=x;
return(0);
intdelete(qqtype*q)//出队
front==q->
rear)
return(null);
front++;
return(q->
front]);
}
intemtype(qqtype*q)
return(-1);
voidbfs(adjlistg,intk,intvisited[])//从顶点k出发进行广度优先遍历
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!
{if(visited[p->
adjves]==0)
{printf(“%d”,g[p->
adjvex].verte);
visited[p->
adjvex]=1;
enter(g.p->
adjvex);
nextarc;
voidtrave(adjlistg,intn)//图采用邻接表存储的广度优先遍历
//数组visited标志图中的定点是否已被访问。
for(i=0;
bfs(g,i,visted);
scanf(“%d”,&
g[k].vertex);
printf(“bfs:
//广度优先遍历图。
实验五查找
//参考程序
stdio.h>
#defineKEYTYPE