MyGraph.h
#pragma once
#include <iostream>
#include <stack>
#include <queue>
using namespace std;
// 邻接矩阵
// 带权有向图
const int MAXSIZE = 20;
const int INFINITE = 100;
template <class T>
class CMyGraph
{
public:
CMyGraph();
~CMyGraph();
private:
T vertexArray[MAXSIZE] = {
0}; // 顶点集合
int edge[MAXSIZE][MAXSIZE]; // 边集合
int numVertex = 0; // 顶点的数量
int numEdge = 0; // 边的数量
private:
bool isGraphEmpty(void) const; // 图是否为空
bool isGraphFull(void) const; // 图是否满了
int getVertexPos(const T& vertex); // 得到顶点的位置,若不存在则返回-1
void errorMessage(int i); // 错误信息
public:
int GetNumOfVertices(void) const; // 获得顶点的数量
int GetNumOfEdge(void) const; // 边的数量
int GetWeight(const T& vertex1, const T& vertex2); //得到边的权重
void InsertVertex(const T& vertex); // 插入顶点
void InsertEdge(const T& vertex1, const T& vertex2, const int weight); // 插入边
void DeleteVertex(const T& vertex); // 删除顶点
void DeleteEdge(const T& vertex1, const T& vertex2); // 删除边
void Display(void); // 显示图
T* DepthFirstSearch(const T& beginVertex); //深度优先搜索
T* BreathFirestSearch(const T& beginVertex);//广度优先搜索
bool PathConnect(const T& vertex1, const T& vertex2); //两顶点之间是否联通
};
MyGraph.cpp
#include "MyGraph.h"
template<class T>
CMyGraph<T>::CMyGraph()
{
for (int i = 0; i < MAXSIZE; i++) {
for (int j = 0; j < MAX