建立用邻接矩阵表示的无向图
#include<stdio.h>
#define NUM 100
typedef struct
{char vexs[NUM];int edges[NUM][NUM];int n,e;
}MGraph;
void CreateMGraph(MGraph *G)
{int i,j,k;printf("请输入顶点数和边数:");scanf("%d,%d",&G->n,&G->e);getchar();printf("请输入顶点信息:");for(i=0;i<G->n;i++)G->vexs[i]=getchar();for(i=0;i<G->n;i++)for(j=0;j<G->n;j++)G->edges[i][j]=0;for(k=0;k<G->e;k++){ printf("请输入边<i,j>的序号");scanf("%d,%d",&i,&j);G->edges[i][j]=1;G->edges[j][i]=1;}printf("图的邻接矩阵为:\n");for(i=0;i<G->n;i++){for(j=0;j<G->n;j++)printf("%3d",G->edges[i][j]);printf("\n");}
}
main()
{MGraph g;CreateMGraph(&g);
}