文档介绍:#include<>
#define UNVISITED 0
#define VISITED 1
class AdjMatrixUndirGraph
{
public:
int vexNum, edgeNum; // 顶点个数和边数
int **Matrix; // 邻接矩阵
char *elems; // 顶点数据
bool *tag;
AdjMatrixUndirGraph(char es[], int vertexNum);
int FirstAdjVex(int v) ; // 返回顶点v的第一个邻接点
int NextAdjVex(int v1, int v2);// 返回顶点v1的相对于v2的下一个邻接点
void Display();
void BFS(int v);
void DFS(int v);
};
AdjMatrixUndirGraph::AdjMatrixUndirGraph(char es[], int vertexNum)
{
vexNum = vertexNum;
edgeNum = 0;
elems = new char[vexNum];
int u, v;
for(v = 0; v < vexNum; v++)
{
elems[v] = es[v];
}
tag = new bool[vexNum]; // 生成标志数组
for(v = 0; v < vexNum; v++)
{ // 初始化标志数组
tag[v] = UNVISITED;
}
Matrix = (int **)new int *[vexNum];// 生成邻接矩阵
for (v = 0; v < vexNum; v++)
{ // 生成邻接矩阵的行
Matrix[v] = new int[vexNum];
}
for (u = 0; u < vexNum; u++)
{
for (v = 0; v < vexNum; v++)
{ // 为邻接矩阵元素赋值
Matrix[u][v] = 0;
}
}
}
int AdjMatrixUndirGraph::FirstAdjVex(int v)
// 操作结果:返回顶点v的第1个邻接点
{
if (v < 0 || v >= vexNum)
{
cout<<"v不合法!";
return -1;
}
for (int cur = 0; cur < vexNum; cur++)
{ // 查找邻接点
if (Matrix[v][cur] != 0) return cur;
}
return -1; // 返回-1表示无邻接点
}
int AdjMatrixUndirGraph::NextAdjVex(int v1, int v2)
// 操作结果:返回顶点v1的相对于v2的下1个邻接点
{
if (v1 < 0 || v1 >= vexNum)
{
cout<<"v1不合法!";
return -1;
}
if (v2 < 0