邻接矩阵
#include<iostream>
#define MAX_VERTS 20
using namespace std;
class Vertex
{
public:
Vertex(char lab) {
Label = lab; }
private:
char Label;
};
class Graph //邻接矩阵
{
public:
Graph();
~Graph();
void addVertex(char lab);
void addEdge(int start, int end);
void printMatrix();
private:
Vertex* vertexList[MAX_VERTS];//数组中为指向顶点的指针
int nVerts; //顶点个数
int adjMat[MAX_VERTS][MAX_VERTS];
};
Graph::Graph()
{
nVerts = 0;
for (int i = 0; i < MAX_VERTS; i++)
{
for (int j = 0; j < MAX_VERTS; j++)
adjMat[i][j] = 0;
}
}
void Graph::addVertex(char lab)
{
vertexList[nVerts] = new Vertex(lab); //创建一个节点
nVerts++;
}
void Graph::addEdge(int start, int end)
{
adjMat[start][end] = 1;
adjMat[end][start] = 1;