编写一个函数OutMGraph(ALGraph G),输出图G的顶点数组元素和邻接表结构数据,对于有权图(网),权值加括号表示。
时间: 2024-03-17 10:46:54 浏览: 21
好的,以下是函数OutMGraph的实现:
```
void OutMGraph(ALGraph G) {
int i, j;
ArcNode* p;
printf("顶点数组元素:\n");
for (i = 0; i < G.vexnum; i++) {
printf("%c ", G.vertices[i].data);
}
printf("\n");
printf("邻接表结构数据:\n");
for (i = 0; i < G.vexnum; i++) {
printf("%c -> ", G.vertices[i].data);
p = G.vertices[i].firstarc;
while (p) {
j = p->adjvex;
if (G.kind == UDG || G.kind == DG) {
printf("%c ", G.vertices[j].data);
} else if (G.kind == UDN || G.kind == DN) {
printf("%c(%d) ", G.vertices[j].data, p->weight);
}
p = p->nextarc;
}
printf("\n");
}
}
```
其中,参数ALGraph G是一个图的邻接表表示,包含以下成员:
- G.vertices:顶点数组
- G.vexnum:顶点数
- G.arcnum:边数
- G.kind:图的类型(有向图、无向图、有向网、无向网)
函数的实现思路为:先输出顶点数组元素,再输出每个顶点的邻接表结构数据。对于有权图(网),权值加括号表示。