话不多说直接上代码s
#include<stdio.h>
#include<stdlib.h>
#define INFINITY 9999
#define MAX_VERTEX_NUM 100
typedef char VertType;//顶点类型
typedef struct {
VertType vexs[MAX_VERTEX_NUM];//顶点表
int arcs[MAX_VERTEX_NUM][MAX_VERTEX_NUM];//邻接矩阵
int vexnum, arcnum;//当前图的顶点数|V|和边数|E|
}MGraph;
//创建邻接矩阵
void CreateMGraph(MGraph* p)
{
printf("请输入顶点数: ");
scanf_s("%d", &p->vexnum);
printf("请输入边数: ");
scanf_s("%d", &p->arcnum);
//图的初始化
for (int i = 0; i < p->vexnum; i++) {
for (int j = 0; j < p->vexnum; j++) {
p->arcs[i][j] = 0;
}
}
printf("请入第几行第几列及对应的权值:");
for (int i = 0; i < p->arcnum; i++) {
int a, b, c;
scanf_s("%d %d %d", &a, &b, &c);
getchar();
p->arcs[a][b] = c;
}
}
void PrintMGraph(MGraph p) {
printf("邻接矩阵是:\n");
for (int i = 0; i < p.vexnum; i++) {
for (int j = 0; j < p.vexnum; j++) {
pri
ntf("%d", p.arcs[i][j]);
}
printf("\n");
}
printf("\n");
}
int main() {
char vexs[MAX_VERTEX_NUM];
MGraph p;
CreateMGraph(&p);
PrintMGraph(p);
return 0;
}
运行结果如下
编译器:Visual 2019