#include<iostream>
using namespace std;
#define MAXVEX 100
typedef char VType;
typedef struct ArcNode
{
int adjvex;
ArcNode *pNextArc;
}ArcNode;
typedef struct VexNode
{
VType data;
ArcNode *pFirstArc;
}VexNode;
typedef struct
{
VexNode VLnode[MAXVEX];
int Vnums;
int Anums;
}ALGraph;
void CreateGraph(ALGraph &G)
{
int i, j, k;
ArcNode *pA;
cin >> G.Vnums >> G.Anums;
for (i = 0; i < G.Vnums; ++i)
{
cin >> G.VLnode[i].data;
G.VLnode[i].pFirstArc = NULL;
}
for (k = 0; k < G.Anums; ++k)
{
cin >> i >> j;
pA = new ArcNode;
pA->adjvex = i;
pA->pNextArc = G.VLnode[j].pFirstArc;
G.VLnode[j].pFirstArc = pA;
pA = new ArcNode;
pA->adjvex = j;
pA->pNextArc = G.VLnode[i].pFirstArc;
G.VLnode[i].pFirstArc = pA;
}
}
void DestroyGraph(ALGraph &G)
{
int i;
ArcNode *p, *q;
for (i = 0; i < G.Vnums; ++i)
{
p = G.VLnode[i].pFirstArc;
while (p)
{
q = p->pNextArc;
delete p;
p = q;
}
}
G.Anums = 0;
}
int LocateVex(ALGraph G, VexNode v)
{
int i;
for (i = 0; i < G.Vnums; ++i)
if (G.VLnode[i].data = v.data)
return i;
return -1;
}
VType GetVex(ALGraph G, int i)
{
if (i >= G.Vnums || i < 0)
exit(-1);
return G.VLnode[i].data;
}
bool PutVex(ALGraph &G, int i, VType value)
{
i = LocateVex(G, G.VLnode[i]);
if (i > -1)
{
G.VLnode[i].data = value;
return true;
}
else
return false;
}
int FirstAdjVex(ALGraph G, VexNode v)
{
int i;
ArcNode *pA;
i = LocateVex(G, v);
pA = G.VLnode[i].pFirstArc;
if (pA)
return pA->adjvex;
else
return -1;
}
int NextAdjVex(ALGraph G, VexNode v, VexNode w)
{
int i, j;
ArcNode *pA;
i = Loca
数据结构之无向图邻接表DFS之创建打印生成森林(整理严蔚敏数据结构)
最新推荐文章于 2023-11-13 18:40:11 发布