数据结构——图
小白专场
常用的存图方式有两种:
一.用邻接矩阵来存图。
二.用邻接表表示图。
此文章我们来介绍第二种表示方法:
用邻接表表示图
1.首先创建一个图结点,包含:头结点,边数,顶点数。
2.边结点,包含:顶点V1、V2,权值。
3.邻接点,包含邻接点下标、边权重。
4.建一个函数来初始化一个空表.
5.建一个函数向其中插入边了。
接下来看看代码实现吧!
#include<stdio.h>
#include<stdlib.h>
#include<math.h>
#include<string.h>
#define MaxVertexNum 100 //这里可以自己定义
typedef int Vertex;
typedef int WeightType;
typedef int DataType;
//..................................
typedef struct AdjVNode *PtrToAdjNode;
struct AdjVNode{//邻接点
Vertex AdjV;//邻接点下标
WeightType Weight;//边权重
PtrToAdjNode Next;
};
//..................................
typedef struct Vnode{//头结点
PtrToAdjNode FirstEdge;
DataType Data;//存顶点的数据
}AdjList[MaxVertexNum];//AdjList是邻接表类型
//..................................
typedef struct ENode* P