#include <stdio.h>
#include <malloc.h>
#define MaxVertexNum 10
typedef struct GNode *PtrToGode;
struct GNode{
int Nv;
int Ne;
int G[MaxVertexNum][MaxVertexNum];
};
typedef PtrToGode MGraph;
typedef int Vertex;
typedef struct ENode *PtrToENode;
struct ENode{
Vertex V1,V2;
int Weight;
};
typedef PtrToENode Edge;
MGraph CreateGraph(int VertexNum);
void InsertEdge(MGraph Graph,Edge E);
MGraph BuildGraph();
MGraph BuildGraph(){
MGraph Graph;
Edge E;
Vertex V;
int Nv,i;
printf("输入顶点数:\n");
scanf("%d",&Nv);
Graph = CreateGraph(Nv);
printf("输入边数:\n");
scanf("%d",&(Graph->Ne));
if(Graph->Ne != 0){
E = (Edge)malloc(sizeof(struct ENode));
for(i = 0;i < Graph->Ne;i++){
printf("输入要插入边的两个顶点以及权重\n");
scanf("%d %d %d",&(E->V1),&(E->V2),(&E->Weight));
InsertEdge(Graph,E);
}
}
return Graph;
}
void InsertEdge(MGraph Graph,Edge E){
Graph->G[E->V1][E->V2] = E->Weight;
Graph->G[E->V2][E->V1] = E->Weight;
}
MGraph CreateGraph(int VertexNum){
int V,W;
MGraph Graph;
Graph = (MGraph)malloc(sizeof(struct GNode));
Graph->Nv = VertexNum;
Graph->Ne = 0;
for(V = 0;V < Graph->Nv;V++){
for(W = 0;W < Graph->Nv;W++){
Graph->G[V][W] = 0;
}
}
return Graph;
}
int main () {
MGraph Graph = BuildGraph();
int i,j;
printf("创建的图的邻接矩阵为:\n");
for(i = 0;i < Graph->Nv;i++){
for(j = 0;j < Graph->Nv;j++){
printf("%d ",Graph->G[i][j]);
}
printf("\n");
}
}