邻接矩阵与邻接表的构建与输出
#include <cstdio>
#include <cstring>
#include <cstdlib>
#define MAXV 6
#define INF 0x3f3f3f3f
using namespace std;
typedef int InfoType;
typedef struct {
int no;
InfoType info;
} VertexType;
typedef struct {
int edges[MAXV][MAXV];
int n, e;
VertexType vexs[MAXV];
} MatrixGraph;
void MatrixGraphInit(MatrixGraph &M, int map[MAXV][MAXV]) {
memset(&M, 0, sizeof(M));
for (int i = 0; i < MAXV; i++) {
M.vexs[i].no = i;
M.n++;
for (int j = 0; j < MAXV; j++) {
M.edges[i][j] = map[i][j];
if (map[i][j] != 0 && map[i][j] != INF) {
M.e++;
M.vexs[i].info++;
}
}
}
}
void MatrixGraphOutput(MatrixGraph &M) {
printf("邻接矩阵信息如下: \n");
printf("\t该邻接矩阵共有%d个结点与%d条弧\n", M.n, M.e);
printf("\t结点信息如下: \n");
for (int i = 0; i < MAXV; i++)
printf("\t\t结点编号%d: 共有%d个出度\n", M.vexs[i].no, M.vexs[i].info);
printf("\t弧的具体信息如下: (行 = 弧头, 列 = 弧尾, value = 边长, 行列从0开始计数)\n");
for (int i = 0; i < MAXV; i++) {
printf("\t\t");
for (int j = 0; j < MAXV; j++)
if (M.edges[i][j] == INF)
printf("%s ", "∞");
else
printf("%d ", M.edges[i][j]);
putchar(10);
}
putchar(10);
}
typedef struct ArcNode {
int adjvex, value;
ArcNode *next;
} ArcNode;
typedef struct {
InfoType info;
ArcNode *first;
} VNode;
typedef struct {
VNode adjacencyList[MAXV];
int n, e;
} ListGraph;
void ListGraphInit(ListGraph &L, int map[MAXV][MAXV]) {
memset(&L, 0, sizeof(L));
for (int i = 0; i < MAXV; i++) {
L.n++;
int mark = 0;
for (int j = 0; j < MAXV; j++) {
if (map[i][j] != 0 && map[i][j] != INF) {
L.e++;
L.adjacencyList[i].info++;
if (!mark) {
mark++;
L.adjacencyList[i].first = (ArcNode *) malloc(sizeof(ArcNode));
L.adjacencyList[i].first->adjvex = j;
L.adjacencyList[i].first->value = map[i][j];
L.adjacencyList[i].first->next = nullptr;
} else {
ArcNode *p = L.adjacencyList[i].first, *q;
while (p)
q = p, p = p->next;
p = (ArcNode *) malloc(sizeof(ArcNode));
q->next = p;
p->value = map[i][j];
p->adjvex = j;
p->next = nullptr;
}
}
}
}
}
void ListGraphOutput(ListGraph &L) {
printf("邻接表信息如下: \n");
printf("\t该邻接表共有%d个结点与%d条边\n", L.n, L.e);
printf("\t结点信息如下: \n");
for (int i = 0; i < MAXV; i++)
printf("\t\t结点编号%d: 共有%d个出度\n", i, L.adjacencyList[i].info);
printf("\t弧的具体信息如下: \n");
for (int i = 0; i < MAXV; i++) {
printf("\t\t%d -%d-> %d", i, L.adjacencyList[i].first->value, L.adjacencyList[i].first->adjvex);
ArcNode *p = L.adjacencyList[i].first->next;
while (p) {
printf(" -%d-> %d", p->value, p->adjvex);
p = p->next;
}
putchar(10);
}
putchar(10);
}
void ListGraphDestory(ListGraph &L) {
for (int i = 0; i < MAXV; i++) {
ArcNode *p = L.adjacencyList[i].first, *q;
while (p) {
q = p->next;
free(p);
p = q;
}
}
printf("邻接表已被释放! \n");
}
int main() {
int m = INF;
MatrixGraph M;
int map[MAXV][MAXV] = {
0, 5, m, 7, m, m,
m, 0, 4, m, m, m,
8, m, 0, m, m, 9,
m, m, 5, 0, m, 6,
m, m, m, 5, 0, m,
3, m, m, m, 1, 0
};
MatrixGraphInit(M, map);
MatrixGraphOutput(M);
ListGraph L;
ListGraphInit(L, map);
ListGraphOutput(L);
ListGraphDestory(L);
return 0;
}
运行结果
E:\Learning\Programme\Jet_Brains\CLionProjects\GraphTest\cmake-build-debug\GraphTest.exe
邻接矩阵信息如下:
该邻接矩阵共有6个结点与10条弧
结点信息如下:
结点编号0: 共有2个出度
结点编号1: 共有1个出度
结点编号2: 共有2个出度
结点编号3: 共有2个出度
结点编号4: 共有1个出度
结点编号5: 共有2个出度
弧的具体信息如下: (行 = 弧头, 列 = 弧尾, value = 边长, 行列从0开始计数)
0 5 ∞ 7 ∞ ∞
∞ 0 4 ∞ ∞ ∞
8 ∞ 0 ∞ ∞ 9
∞ ∞ 5 0 ∞ 6
∞ ∞ ∞ 5 0 ∞
3 ∞ ∞ ∞ 1 0
邻接表信息如下:
该邻接表共有6个结点与10条边
结点信息如下:
结点编号0: 共有2个出度
结点编号1: 共有1个出度
结点编号2: 共有2个出度
结点编号3: 共有2个出度
结点编号4: 共有1个出度
结点编号5: 共有2个出度
弧的具体信息如下:
0 -5-> 1 -7-> 3
1 -4-> 2
2 -8-> 0 -9-> 5
3 -5-> 2 -6-> 5
4 -5-> 3
5 -3-> 0 -1-> 4
邻接表已被释放!
Process finished with exit code 0