-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSimpleGraph.h
52 lines (43 loc) · 942 Bytes
/
SimpleGraph.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
//
// Created by Ryan.Zurrin001 on 1/9/2022.
//
#ifndef GRAPH_CPP_SIMPLEGRAPH_H
#define GRAPH_CPP_SIMPLEGRAPH_H
#include <bits/stdc++.h>
using namespace std;
template <typename T>
class SimpleGraph {
private:
int V{};
int E{};
list<T> *adj;
public:
explicit SimpleGraph(int V);
void addEdge(int v, int w, bool undirected = true);
void printAdjList();
};
template<typename T>
SimpleGraph<T>::SimpleGraph(int V) {
this->V = V;
this->E = 0;
adj = new list<T>[V];
}
template<typename T>
void SimpleGraph<T>::addEdge(int v, int w, bool undirected) {
adj[v].push_back(w);
if (undirected) {
adj[w].push_back(v);
}
E++;
}
template<typename T>
void SimpleGraph<T>::printAdjList() {
for (int i = 0; i < V; i++) {
cout << i << "-->";
for (auto it : adj[i]) {
cout << it << ", ";
}
cout << endl;
}
}
#endif //GRAPH_CPP_SIMPLEGRAPH_H