C++数据结构 (图)邻接矩阵模板
一、成品代码
#include<iostream>
using namespace std;#define inf -1class Graph {
private:int vertices;int** edges;
public:Graph(int vertices);~Graph();void addEdge(int u, int v, int w);//代表往这个图中添加一条从u到v的边,边的权为wvoid printGraph();
};//构造函数
Graph::Graph(int vertices) {this->vertices = vertices;//顶点个数edges = new int* [vertices];//二维数组,一个指针数组,每个指针指向一个数组//初始化数组for (int i = 0; i < vertices; i++) {edges[i] = new int[vertices];for (int j = 0; j < vertices; j++) {edges[i][j] = inf;}}
}//析构函数
Graph::~Graph() {for (int i = 0; i < vertices; i++) {delete[] edges[i];}delete[] edges;
}void Graph::addEdge(int u, int v, int w) {edges[u][v] = w;
}
void Graph::printGraph() {for (int i = 0; i < vertices; i++) {for (int j = 0; j < vertices; j++) {cout << edges[i][j] << " ";}cout << endl;}
}int main() {int vertices = 5;Graph graph(vertices);graph.addEdge(0, 1, 1);graph.addEdge(0, 2, 3);graph.addEdge(1, 2, 2);graph.addEdge(2, 3, 7);graph.addEdge(3, 4, 9);graph.addEdge(4, 0, 4);graph.addEdge(4, 2, 5);graph.printGraph();return 0;
}
二、构造过程
(1)我们的目标是构造一个图,一个图需要由顶点和线构成,图可以由邻接矩阵来表示,矩阵我们又可以由二维数组表示。因此我们的类需要有两个元素,顶点个数和一个二维数组。我们还需要连线的方法和打印图的方法。其中连线的方法我们需要知道线是从谁连向谁,权重是多少。构造函数时需要知道有多少个顶点,构造出空的图。
#include<iostream>
using namespace std;class Graph {int vertices;int** edges;Graph(int vertices);~Graph();void addGraph(int u,int v,int w);void printGraph();
};
(2)我们先实现构造函数的方法,我们需要或的一个vertices大小的指针数组,数组中的每个元素是一个vertices大小的数组。并将数组中每个元素初始化为空(指定任意不可能取到的值即可)。
#include<iostream>
using namespace std;#define inf -1
class Graph {int vertices;int** edges;Graph(int vertices);~Graph();void addGraph(int u,int v,int w);void printGraph();
};Graph::Graph(int vertices) {this->vertices = vertices;edges = new int* [vertices];for (int i = 0; i < vertices; i++) {edges[i] = new int[vertices];for (int j = 0; j < vertices; j++) {edges[i][j] = inf;}}
}
Graph::~Graph() {}
void Graph::addGraph(int u, int v, int w) {}
void Graph::printGraph() {}
(3)析构函数则是遍历指针数组,将每个元素一一删除,再将指针数组删除
#include<iostream>
using namespace std;#define inf -1
class Graph {int vertices;int** edges;Graph(int vertices);~Graph();void addGraph(int u,int v,int w);void printGraph();
};Graph::Graph(int vertices) {this->vertices = vertices;edges = new int* [vertices];for (int i = 0; i < vertices; i++) {edges[i] = new int[vertices];for (int j = 0; j < vertices; j++) {edges[i][j] = inf;}}
}
Graph::~Graph() {for (int i = 0; i < vertices; i++) {delete[]edges[i];}delete edges;
}
void Graph::addGraph(int u, int v, int w) {}
void Graph::printGraph() {}
(4)连线直接给二维数组对应索引赋值即可
#include<iostream>
using namespace std;#define inf -1
class Graph {int vertices;int** edges;Graph(int vertices);~Graph();void addGraph(int u,int v,int w);void printGraph();
};Graph::Graph(int vertices) {this->vertices = vertices;edges = new int* [vertices];for (int i = 0; i < vertices; i++) {edges[i] = new int[vertices];for (int j = 0; j < vertices; j++) {edges[i][j] = inf;}}
}
Graph::~Graph() {for (int i = 0; i < vertices; i++) {delete[]edges[i];}delete edges;
}
void Graph::addGraph(int u, int v, int w) {edges[u][v] = w;
}
(5)打印就是简单的遍历即可
#include<iostream>
using namespace std;#define inf -1
class Graph {int vertices;int** edges;Graph(int vertices);~Graph();void addGraph(int u,int v,int w);void printGraph();
};Graph::Graph(int vertices) {this->vertices = vertices;edges = new int* [vertices];for (int i = 0; i < vertices; i++) {edges[i] = new int[vertices];for (int j = 0; j < vertices; j++) {edges[i][j] = inf;}}
}
Graph::~Graph() {for (int i = 0; i < vertices; i++) {delete[]edges[i];}delete edges;
}
void Graph::addGraph(int u, int v, int w) {edges[u][v] = w;
}
void Graph::printGraph() {for (int i = 0; i < vertices; i++) {for (int j = 0; j < vertices; j++) {cout << edges[i][j] << " ";}cout << endl;}
}