在建立邻接表时,若输入的顶点信息即为顶点的编号,则建立邻接表的时间复杂度为O(n+e),否则需要通过查找才能得到顶点在图中位置,则时间复杂度为O(n*e)
在邻接表上容易找到任一顶点的第一个邻接点和下一个邻接点,但要判定任意两个顶点之间是否有边,则需要搜索该链表,不及邻接矩阵方便。
#include <stdio.h>
#include <stdlib.h>
#include <limits.h>
#define MAX_VERTEX_NUM 20
#define INFINITY INT_MAX
typedef int Status;
#define OK 1
#define ERROR 0
typedef int InfoType; //定义弧相关信息为整形
typedef char VertexType; //定义顶点类型为字符型
typedef enum {DG,DN,UDG,UDN} GraphKind; //定义{有向图,有向网,无向图,无向网}等枚举常量,同时定义GraphKind为枚举变量类型
typedef struct ArcNode
{
int adjvex; //该弧所指向的顶点在顺序结构中的位置
struct ArcNode *nextarc; //与该弧邻接(同一尾结点)的下一条弧的指针
InfoType *info; //该弧相关信息的指针
}ArcNode; //弧结构
typedef struct VNode
{
VertexType data; //顶点
ArcNode *firstarc; //指向第一条依附该顶点的弧的指针
}VNode,AdjList[MAX_VERTEX_NUM]; //顶点结构
typedef struct
{
AdjList vertices; //顶点数组
int vexnum,arcnum; //顶点数和弧数
GraphKind kind;
}ALGraph; //邻接表表示的图结构
Status CreateGraph(ALGraph *G);
Status CreateDG(ALGraph *G);
Status CreateDN(ALGraph *G);
Status CreateUDG(ALGraph *G);
Status CreateUDN(ALGraph *G);
int LocateVex(AL