feat: Add C++ code for the graph bfs and dfs (#401)
* Add C++ code for the graph bfs and dfs * Add C++ code for the graph bfs and dfspull/402/head
parent
4f941e3d99
commit
33c797efeb
@ -0,0 +1,49 @@
|
||||
/**
|
||||
* 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;
|
||||
}
|
@ -0,0 +1,34 @@
|
||||
/**
|
||||
* File: PrintUtil.hpp
|
||||
* Created Time: 2023-03-02
|
||||
* Author: Krahets (krahets@163.com)
|
||||
*/
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <vector>
|
||||
using namespace std;
|
||||
|
||||
/* 顶点类 */
|
||||
struct Vertex {
|
||||
int val;
|
||||
Vertex(int x) : val(x) {}
|
||||
};
|
||||
|
||||
/* 输入值列表 vals ,返回顶点列表 vets */
|
||||
vector<Vertex*> valsToVets(vector<int> vals) {
|
||||
vector<Vertex*> vets;
|
||||
for (int val : vals) {
|
||||
vets.push_back(new Vertex(val));
|
||||
}
|
||||
return vets;
|
||||
}
|
||||
|
||||
/* 输入顶点列表 vets ,返回值列表 vals */
|
||||
vector<int> vetsToVals(vector<Vertex*> vets) {
|
||||
vector<int> vals;
|
||||
for (Vertex *vet : vets) {
|
||||
vals.push_back(vet->val);
|
||||
}
|
||||
return vals;
|
||||
}
|
Loading…
Reference in new issue