数据结构与算法Word文档下载推荐.docx
- 文档编号:22314840
- 上传时间:2023-02-03
- 格式:DOCX
- 页数:35
- 大小:255.38KB
数据结构与算法Word文档下载推荐.docx
《数据结构与算法Word文档下载推荐.docx》由会员分享,可在线阅读,更多相关《数据结构与算法Word文档下载推荐.docx(35页珍藏版)》请在冰豆网上搜索。
typedefcharVertexData;
typedefintEdgeData;
typedefstruct{
VertexDatavexlist[NumVertices];
EdgeDataedge[NumVertices][NumVertices];
intn;
inte;
}MTGraph;
voidIniMGraph(MTGraph*G)
{
for(inti=0;
i<
NumVertices;
i++)
for(intj=0;
j<
j++)
G->
edge[i][j]=0;
G->
n=0;
e=0;
}
voidNewNode(MTGraph*G,VertexDatav)
vexlist[G->
n]=v;
n++;
voidDelNode(MTGraph*G,intm)
inti,j;
if(G->
n==0||m>
=NumVertices)
return;
for(i=m;
G->
n-1;
G->
vexlist[i]=G->
vexlist[i+1];
for(i=0;
n;
{
if(G->
edge[i][m]!
=0)
e--;
}
for(j=0;
edge[i][j]=G->
edge[i+1][j];
edge[j][i]=G->
edge[j][i+1];
n--;
voidSetSucc(MTGraph*G,intv1,intv2,EdgeDataw)
{
if(!
IsEdge(G,v1,v2))
edge[v1][v2]=w;
edge[v2][v1]=w;
e++;
}
voidDelSucc(MTGraph*G,intv1,intv2)
if(IsEdge(G,v1,v2))
edge[v1][v2]=0;
edge[v2][v1]=0;
BOOLEANIsEdge(MTGraph*G,intv1,intv2)
if(v1>
=0&
&
v1<
n&
v2>
v2<
v1!
=v2)
edge[v1][v2]!
returnTRUE;
else
returnFALSE;
returnFALSE;
voidCreateMGraph(MTGraph*G)
inti,j,k,w;
cout<
<
"
输入顶点数和边数:
;
cin>
>
n>
e;
输入顶点的字符:
for(i=0;
i++)
cin>
vexlist[i];
for(j=0;
j<
j++)
for(k=0;
k<
k++){
cout<
输入一条边的两个顶点:
"
i>
j>
w;
edge[i][j]=w;
edge[j][i]=w;
}
voidCreateMGraph(MTGraph*G,VertexDataV[],EdgeDataE[][NumVertices],intn)
n=n;
vexlist[i]=V[i];
for(j=i;
{
if(E[i][j]!
{
G->
edge[i][j]=E[i][j];
edge[j][i]=E[i][j];
}
}
voidPrintMT(MTGraph*G)
'
\t'
vexlist[i]<
endl;
cout<
edge[i][j]<
voidmain()
MTGraphG;
IniMGraph(&
G);
VertexDatav[6]={'
v1'
'
v2'
v3'
v4'
v5'
'
v6'
};
EdgeDatae[6][NumVertices]={
{0,1,0,1,0,1},
{1,0,1,1,1,0},
{0,1,0,0,1,0},
{1,1,0,0,1,1},
{0,1,1,1,0,0}
{1,0,0,1,0,0}};
CreateMGraph(&
G,v,e,6);
NewNode(&
G,'
);
SetSucc(&
G,0,1,1);
G,0,3,1);
G,1,3,1);
G,1,2,1);
G,2,3,1);
PrintMT(&
DelSucc(&
G,1,3);
DelNode(&
G,2);
邻接表表示:
typedefstructnode{
intadjvex;
EdgeDatacost;
structnode*next;
}EdgeNode;
typedefstruct{
VertexDatavertex;
EdgeNode*firstedge;
}VertexNode;
VertexNodevexlist[NumVertices+1];
intn,e;
}AdjGraph;
voidIniADJGraph(AdjGraph*G)
for(inti=1;
vexlist[i].vertex=freever;
vexlist[i].firstedge=NULL;
BOOLEANNewNode(AdjGraph*G,VertexDatav)
n<
NumVertices-1)
for(inti=1;
{
if(G->
vexlist[i].vertex==freever)
vexlist[i].vertex=v;
returnTRUE;
voidDelNode(AdjGraph*G,intm)
vexlist[m].vertex!
=freever)
vexlist[m].vertex=freever;
EdgeNode*p=G->
vexlist[m].firstedge;
while(p!
=NULL)
DelSucc(G,m,p->
adjvex);
p=G->
vexlist[m].firstedge=NULL;
BOOLEANSetSucc(AdjGraph*G,intv1,intv2,EdgeDataw)
vexlist[v1].vertex!
=freever&
vexlist[v2].vertex!
=freever)
vexlist[v1].firstedge;
if(IsEdge(G,v1,v2))
DelSucc(G,v1,v2);
p=newEdgeNode;
p->
adjvex=v2;
p->
cost=w;
next=G->
vexlist[v1].firstedge=p;
adjvex=v1;
vexlist[v2].firstedge;
vexlist[v2].firstedge=p;
returnTRUE;
voidDelSucc(AdjGraph*G,intv1,intv2)
if(IsEdge(G,v1,v2))
vexlist[v1].firstedge,*p1;
if(p->
adjvex==v2)
p1=p;
vexlist[v1].firstedge=p->
next;
deletep1;
do
p1=p->
if(p1==NULL)
break;
if(p1->
{
p->
next=p1->
deletep1;
G->
}
p=p->
}while(p!
=NULL);
p=G->
adjvex==v1)
vexlist[v2].firstedge=p->
BOOLEANIsEdge(AdjGraph*G,intv1,intv2)
if(p==NULL)
while
(1)
if(p->
p=p->
if(p==NULL)
returnFALSE;
returnFALSE;
voidCreateADJGraph(AdjGraph*G)
cin>
n>
for(inti=1;
i<
=G->
i++)
{
输入第"
i<
个顶点信息:
cin>
vexlist[i].vertex;
vexlist[i].firstedge=NULL;
for(i=1;
inttail,head;
EdgeDataweight;
条边的起始点、终止点以及权重:
tail>
head>
weight;
EdgeNode*p=newEdgeNode;
adjvex=head;
cost=weight;
vexlist[tail].firstedge;
vexlist[tail].firstedge=p;
adjvex=tail;
vexlist[head].firstedge;
vexlist[head].firstedge=p;
voidCreateADJGraph(AdjGraph*G,VertexDataV[],EdgeDataE[][NumVertices],intn)
=n;
vexlist[i].vertex=V[i-1];
=n;
for(intj=i+1;
{
if(E[i-1][j-1]!
EdgeNode*p=newEdgeNode;
p->
adjvex=j;
cost=E[i-1][j-1];
vexlist[i].firstedge;
vexlist[i].firstedge=p;
p=newEdgeNode;
adjvex=i;
vexlist[j].firstedge;
vexlist[j].firstedge=p;
}
voidPrintADJ(AdjGraph*G)
EdgeNode*p;
EdgeDataresult[NumVertices][NumVertices]={0};
VertexDataver[NumVertices];
for(i=1;
vexlist[i].vertex<
ver[i]=G->
{
=NULL)
result[i][p->
adjvex]=p->
cost;
ver[i]<
for(j=1;
result[i][j]<
无向图:
AdjGraphG;
IniADJGraph(&
EdgeDatae[6][NumVertices]={{0,1,0,1,0,1},
{0,1,1,1,0,0},
CreateADJGraph(&
G.n<
'
G.e<
v4);
G,1,4,1);
G,1,5,1);
G,2,4,1);
G,3,4,1);
G,3,5,1);
G,4,5,1);
G,2,4,3);
G,3,4);
PrintADJ(&
四、已知一个有向图如下图所示,试给出图的邻接矩阵和邻接表存储示意图(分别用矩阵和数组链表图表示),并编程分别实现该图的邻接矩阵表示和邻接表表示,要求编写两种表示方法的存储结构、相关基本操作,并在主函数中创建该图。
邻接矩阵:
邻接表存储示意图:
EdgeDataedge[NumVertices][NumVertices];
intn;
inte;
voidIniMGraph_direct
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- 数据结构 算法