forked from rahul22mrk/hackoctoberfest2021
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathadj_list_graph.cpp
More file actions
56 lines (43 loc) · 818 Bytes
/
adj_list_graph.cpp
File metadata and controls
56 lines (43 loc) · 818 Bytes
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
53
54
55
56
#include <iostream>
#include <list>
using namespace std;
class graph {
private:
int V;
list<int> *adjlist;
public:
graph(int v);
void addEdge(int u, int v, bool bidir);
void print();
};
graph::graph(int v) {
this->V = v;
adjlist = new list<int>[V];
}
void graph::addEdge(int u, int v, bool bidir = true) {
adjlist[u].push_back(v);
if (bidir) {
adjlist[v].push_back(u);
}
}
void graph::print() {
for (int i=0 ; i<V ; i++) {
cout << i << " -> ";
for (int node:adjlist[i]) {
cout << " ";
cout << node << ", ";
}
cout << endl;
}
}
int main()
{
graph g(4);
g.addEdge(0, 1);
g.addEdge(0, 2);
g.addEdge(0, 3);
g.addEdge(1, 3);
g.addEdge(3, 2);
g.print();
return 0;
}