cpp-algo-cases/chapter_graph/graph_adjacency_list_test.cpp

50 lines
1.3 KiB
C++

/**
* File: graph_adjacency_list_test.cpp
* Created Time: 2023-02-09
* Author: what-is-me (whatisme@outlook.jp), krahets (krahets@163.com)
*/
#include "./graph_adjacency_list.cpp"
/* Driver Code */
int main() {
/* 初始化无向图 */
vector<Vertex *> v = valsToVets(vector<int>{1, 3, 2, 5, 4});
vector<vector<Vertex *>> edges = {{v[0], v[1]}, {v[0], v[3]}, {v[1], v[2]},
{v[2], v[3]}, {v[2], v[4]}, {v[3], v[4]}};
GraphAdjList graph(edges);
cout << "\n初始化后,图为" << endl;
graph.print();
/* 添加边 */
// 顶点 1, 2 即 v[0], v[2]
graph.addEdge(v[0], v[2]);
cout << "\n添加边 1-2 后,图为" << endl;
graph.print();
/* 删除边 */
// 顶点 1, 3 即 v[0], v[1]
graph.removeEdge(v[0], v[1]);
cout << "\n删除边 1-3 后,图为" << endl;
graph.print();
/* 添加顶点 */
Vertex *v5 = new Vertex(6);
graph.addVertex(v5);
cout << "\n添加顶点 6 后,图为" << endl;
graph.print();
/* 删除顶点 */
// 顶点 3 即 v[1]
graph.removeVertex(v[1]);
cout << "\n删除顶点 3 后,图为" << endl;
graph.print();
// 释放内存
for (Vertex *vet : v) {
delete vet;
}
return 0;
}