前言
建立图的邻接表存储结构,并以以0结点为起点实现上述图的深度优先和广度优先遍历算法;
邻接表的建立:
邻接表存储图的实现方式是,给图中的各个顶点独自建立一个链表,用节点存储该顶点,用链表中其他节点存储各自的邻接点。
#include<iostream>
using namespace std;
#define MAX_VERTEX_NUM 20//最大顶点个数
#define VertexType int//顶点数据的类型
#define InfoType int//图中弧或者边包含的信息的类型
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;//记录图中顶点数和边或弧数
}ALGraph;
//判断该点在数组中的位置
int LocateVex(ALGraph G, int V)
{
for (int i = 0; i < G.vexnum; ++i)
{
if (G.vertices[i].data == V)
{
return i;
}
}
return -1;
}
void CreateUDG(ALGraph& G)
{
//采用邻接表创建无向图
cin >> G.vexnum