首先看图的邻接矩阵表示
用到的数据结构
#define MaxVertexNum 100
typedef char VertexType ; //由用户定义顶点类型
typedef int EdgeType; //由用户定义边上的权值类型
typedef struct
{
VertexType vexs[MaxVertexNum];
EdgeType edges[MaxVertexNum][MaxVertexNum];
int n,e;
}MGrap;
顶点集合一维数组
边的权值集合二维数组
顶点数和边数
遇到的问题,在输入顶点时,输入的是字符,用的是getchar(). 输入完后,缓冲区不清楚,导致输入的不正确,使用fflush(stdin).
以下代码创建的是无向图。
#include <stdio.h>
#include <stdlib.h>
#define MaxVertexNum 100
typedef char VertexType ; //由用户定义顶点类型
typedef int EdgeType; //由用户定义边上的权值类型
typedef struct
{
VertexType vexs[MaxVertexNum];
EdgeType edges[MaxVertexNum][MaxVertexNum];
int n,e;
}MGrap;
void createMGrap(MGrap*);
void main()
{
MGrap *G =(MGrap*)