邻接矩阵问题,大家快来看看啦!
//用邻接矩存储一个图#include<stdio.h>
#include<stdlib.h>
#define MAX 30
typedef struct{
char vexs[MAX];
int edges[MAX][MAX];
int n;
int e;
}MGragh;
//创建一个矩阵图
void CreateGragh(MGragh *G){
G = (MGragh*)malloc(sizeof(MGragh));
int i = 0, j = 0, k = 0, w = 0;
char ch ='0';
//输入顶点个数,边数个数
printf("输入顶点的个数和边数的个数:\n");
scanf("%d%d", &(G -> n), &(G -> e));
printf("输入图的顶点(字符):\n");
for(i = 0; i < G -> n; i++){
scanf("%c", &(G -> vexs[i]));
}
//给矩阵赋初值
for(i = 0; i < G -> n; i++){
for(j = 0; j < G -> n; j++){
G -> edges[i][j] = 0;
}
}
//输入e条边,建立邻接矩阵
for(k = 0; k < G -> e; k++){
scanf("%d%d",&i, &j);
G -> edges[i][j] = 1;
}
//输出矩阵
printf("输出邻接矩阵:\n");
for(i = 0; i < G -> n; i++){
for(j = 0; j < G -> n; j++){
printf("%d ", G-> edges[i][j]);
if(j == G -> n - 1)
printf("\n");
}
}
}
//main 函数
void main(){
int i = 0;
int j = 0;
MGragh G;
CreateGragh(&G);
}
/*
为什么输入e条边,建立邻接矩阵
for(k = 0; k < G -> e; k++){
scanf("%d%d",&i, &j);
G -> edges[i][j] = 1;
}
不会执行
搞的我累死了,还是没有出来,老是输出全为0的数!
高手们给个回答!