邻接表与邻接矩阵相互转换
作者:
qbz666
,
2023-11-30 00:23:07
,
所有人可见
,
阅读 137
#include <bits/stdc++.h>
using namespace std;
#define MAXN 1100
typedef struct
{
int vernum;
int edgenum;
int vertex[MAXN];
int edges[MAXN][MAXN];
}MGraph;
typedef struct edgeNode
{
int adjvex;//e
struct edgeNode *nextArc;//ne
int info;//w
}edgeNode;
typedef struct VNode
{
int data;
edgeNode *firstArc;//h
}VNode;
typedef struct
{
int vernum;
int edgenum;
VNode adjvex[vernum];
}ALGraph;
//图的邻接表转邻接矩阵
void ALGraph_to_MGraph(ALGraph AG,MGraph &MG)
{
MG.vernum=AG.vernum;
MG.edgenum=AG.edgenum;
for(int i=0;i<AG.vernum;i++)
{
for(edgeNode *p=AG.adjvex[i].firstArc;p;p=p->nextArc)
{
MG.edges[i][p->adjvex]=1;
}
MG.vernum[i]=AG.adjvex[i].data;
}
}
//图的邻接矩阵转邻接表
void MGraph_to_ALGraph(ALGraph &AG,MGraph MG)
{
AG.vernum=MG.vernum;
AG.edgenum=MG.edgenum;
for(int i=0;i<vernum;i++)
{
for(int j=0;j<vernum;j++)
{
if(MG.edgenum[i][j])
{
edgeNode *p=(edgeNode*)malloc(sizeof (struct edgeNode));
p->adjvex=j;
p->nextArc=AG.adjvex[i].firstArc;
AG.adjvex[i].firstArc=p;
}
}
AG.vertex[i]=MG.vertex[i];
}
}
int main()
{
}