[数据结构_3]图的邻接矩阵与邻接表

//ALGraph.h
//完整的图邻接表类型
#define Maxve 6
#define INF 32767
typedef int InfoType;
typedef int Vertex;
typedef struct ANode
{
int adjvex;
struct ANode *nextarc;
InfoType info;
}ArcNode;
typedef struct Vnode
{
Vertex data;
ArcNode *firstarc;
}VNode;
typedef VNode AdjList[Maxve];//AdjList为邻接表类型
typedef struct
{
AdjList adjlist;
int n,e;

}ALGraph;

//MGraph.h
//图邻接矩阵数据类型定义
#define MAXV 6
typedef struct
{
int no;
InfoType info;
} VertexType;
typedef struct
{
int edges[MAXV][MAXV];
int n,e;
VertexType vexs[MAXV];
} MGraph;

//不带权图
//1.邻接矩阵转换为邻接表算法
//2.邻接表转换为邻接矩阵算法
#include"ALGraph.h"
#include"MGraph.h"
#include
#include
extern void DispMat(MGraph g);
extern void DispAdj(ALGraph *G);
//邻接矩阵转换为邻接表算法
void MatToList(MGraph g,ALGraph *&G)
{
int i,j;
ArcNode *p;
G=(ALGraph *)malloc(sizeof(ALGraph));
for(i=0;iG->adjlist[i].firstarc=NULL;
for(i=0;ifor(j=g.n-1;j>=0;j--)
{
if(g.edges[i][j]!=0)
{
p=(ArcNode *)malloc(sizeof(ArcNode));
p->adjvex=j;
p->nextarc=G->adjlist[i].firstarc;
G->adjlist[i].firstarc=p;
}
}
G->n=g.n;
G->e=g.e;
}
//邻接表转换为邻接矩阵算法
void ListToMat(ALGraph *G,MGraph &g)
{
int i;
ArcNode *p;
for(i=0;in;i++)
{
p=G->adjlist[i].firstarc;
while(p!=NULL)
{
g.edges[i][p->adjvex]=1;
p=p->nextarc;
}
}
g.n=G->n;
g.e=G->e;
}
//输出邻接矩阵g
void DispMat(MGraph g)
{
int i,j;
for(i=0;i{
for(j=0;jif(g.edges[i][j]==INF)//怎样存储无穷大
printf("%3s","∞");
else
printf("%3d",g.edges[i][j]);
printf("\n");
}
}
//输出邻接表G
void DispAdj(ALGraph *G)
{
int i;
ArcNode *p;
for(i=0;in;i++)
{
p=G->adjlist[i].firstarc;
printf("%3d: ",i);
while(p!=NULL)
{
printf("%3d(%d)",p->adjvex,p->info);
p=p->nextarc;
}
printf("\n");
}
}
void main()
{
int i,j;
ALGraph *G;
MGraph g;
g.n=6;
g.e=10;
int A[6][6]={
{0,5,INF,7,INF,INF},
{INF,0,4,INF,INF,INF},
{8,INF,0,INF,INF,9},
{INF,INF,5,0,INF,6},
{INF,INF,INF,5,0,INF},
{3,INF,INF,INF,1,0}};
for(i=0;i<6;i++)
for(j=0;j<6;j++)
g.edges[i][j]=A[i][j];
printf("输出有向图邻接矩阵为:\n");
DispMat(g);
G=(ALGraph*) malloc(sizeof(ALGraph));
printf("有向图邻接矩阵g转换为邻接表G为:\n");
MatToList(g,G);
DispAdj(G);
printf("有向图邻接表G转换为邻接矩阵g为:\n");
ListToMat(G,g);
DispMat(g);
}

//带权图
//1.邻接矩阵转换为邻接表算法
//2.邻接表转换为邻接矩阵算法
#include"ALGraph.h"
#include"MGraph.h"
#include
#include
//邻接矩阵转换为邻接表算法
void MatToList(MGraph g,ALGraph *&G)
{
int i,j;
ArcNode *p;
G=(ALGraph *)malloc(sizeof(ALGraph));
for(i=0;iG->adjlist[i].firstarc=NULL;
for(i=0;ifor(j=g.n-1;j>=0;j--)
{
if(g.edges[i][j]!=0)
{
p=(ArcNode *)malloc(sizeof(ArcNode));
p->adjvex=j;
p->info=g.edges[i][j];
p->nextarc=G->adjlist[i].firstarc;
G->adjlist[i].firstarc=p;
}
}
G->n=g.n;
G->e=g.e;
}
//邻接表转换为邻接矩阵算法
void ListToMat(ALGraph *G,MGraph &g)
{
int i;
ArcNode *p;
for(i=0;in;i++)
{
p=G->adjlist[i].firstarc;
while(p!=NULL)
{
g.edges[i][p->adjvex]=p->info;
p=p->nextarc;
}
}
g.n=G->n;
g.e=G->e;
}
//输出邻接矩阵g
void DispMat(MGraph g)
{
int i,j;
for(i=0;i{
for(j=0;jif(g.edges[i][j]==INF)//怎样存储无穷大
printf("%3s","∞");
else
printf("%3d",g.edges[i][j]);
printf("\n");
}
}
//输出邻接表G
void DispAdj(ALGraph *G)
{
int i;
ArcNode *p;
for(i=0;in;i++)
{
p=G->adjlist[i].firstarc;
printf("%3d: ",i);
while(p!=NULL)
{
if(p->info!=INF)
printf("%3d(%d)",p->adjvex,p->info);
p=p->nextarc;
}
printf("\n");
}
}
void main()
{
int i,j;
ALGraph *G;
MGraph g;
g.n=6;
g.e=10;
int A[6][6]={
{0,5,INF,7,INF,INF},
{INF,0,4,INF,INF,INF},
{8,INF,0,INF,INF,9},
{INF,INF,5,0,INF,6},
{INF,INF,INF,5,0,INF},
{3,INF,INF,INF,1,0}};
for(i=0;i<6;i++)
for(j=0;j<6;j++)
g.edges[i][j]=A[i][j];
printf("输出有向图邻接矩阵为:\n");
DispMat(g);
G=(ALGraph*) malloc(sizeof(ALGraph));
printf("有向图邻接矩阵g转换为邻接表G为:\n");
MatToList(g,G);
DispAdj(G);
printf("有向图邻接表G转换为邻接矩阵g为:\n");
ListToMat(G,g);
DispMat(g);
}

你可能感兴趣的:(数据结构与算法)