图邻接矩阵:用两个数组来表示图。一个一维数组存储图的顶点,另外一个二维数组存储图中边或者弧的信息。
首先需要创建一个图,并在创建的过程记住每个元素的位置信息,这样有利于用easyx来画出更直观的图,便于观察。需要定义两个结构体:
元素即其位置结构体,图的结构体
用到的头文件如下:
#include <stdio.h>
#include <stdlib.h>
#include <memory.h>
#include <graphics.h>
#include <conio.h>
#define MAXVEX 100
typedef int EdgeType;
#define INFINITY 0x7FFFFFFF
typedef struct
{
char data;
int x;
int y;
}VertexType;
typedef struct
{
VertexType vexs[MAXVEX];
EdgeType arc[MAXVEX][MAXVEX];
int numVertexes, numEdges;
}MGraph;
由于每次都要输入元素个数,元素位置等比较麻烦,所以把需要输入的数据放在一个同级目录下文本中,这样方便快捷。
输入数据函数(取数据函数)
void CreateMGraph(MGraph* g)
{
FILE* pf = fopen("MGraphW.txt", "r");
fs