关于邻接矩阵、邻接表的具体包含哪些数据我就不说了,鄙人时间有点赶,大家自行百度下哈。
程序中图的例子:
2.1号:补充,突然发现我的程序,边的权重只能为1位数,如果权重为多位数,只取个位数的值,因此需要进行下面的修改:(邻接矩阵、邻接表都一样)
1) weight初始化为0(以前没有初始化)
2)从控制台得到:weight = weight * 10 + int(c - ‘0’);(以前为:weight = int(c - ‘0’);)
这样weight就可以是int范围内的任意正整数值了。
GNode.h
#define MAXSIZE 10
//邻接矩阵
typedef struct AMGraph {
char** vexs;
int** arcs;
int vexnum, arcnum;
}AMGraph;
//邻接表
typedef struct OtherInfo {
//权重等
int weight;
}OtherInfo;
//边结点
typedef struct ArcNode {
int index;
struct ArcNode* next;
OtherInfo info;
}ArcNode;
//表头结点
typedef struct VNode {
char* data;
struct ArcNode* first;
}VNode;
typedef struct ALGraph {
VNode* vertices;
int vexnum, arcnum;
}ALGraph;
main.cpp
#include<stdio.h>
#include<stdlib.h>
#include <string.h>
#include"GNode.h"
//邻接矩阵
void CreateAMGraph(AMGraph *g); //创建邻接矩阵
void GetIndex(char** vex, int size, char* resource, int &result); //根据输入顶点信息得到顶点的索引
bool Equal(char* s1, char* s2); //判断两个字符串是否相等
void PrintAMGraphMatrix(AMGraph *g); //输出邻接矩阵
//邻接表
void CreateALGraph(ALGraph* g); //创建邻接表
void GetIndex_s(ALGraph* g, int size, char* resource, int &result); //根据输入顶点信息得到顶点的索引
void PrintfALGraph(ALGraph* g); //输出邻接表
int main() {
//邻接矩阵
printf("*******邻接矩阵******:\n");
AMGraph* g;
g = (AMGraph*)malloc(sizeof(AMGraph));
CreateAMGraph(g); //创建邻接矩阵
PrintAMGraphMatrix(g); //输出矩阵