-
Notifications
You must be signed in to change notification settings - Fork 242
/
Copy pathImplementationOfGraph.cpp
50 lines (42 loc) · 1.17 KB
/
ImplementationOfGraph.cpp
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
#include<iostream>
#include<list>
#include<unordered_map>
using namespace std;
template <typename T>
class Graph{
public:
unordered_map<T,list<T>> edges;
void insert(T firstEdgeElement, T secondEdgeElement, bool direction){
if(direction){
edges[firstEdgeElement].push_back(secondEdgeElement);
}else{
edges[firstEdgeElement].push_back(secondEdgeElement);
edges[secondEdgeElement].push_back(firstEdgeElement);
}
}
void print(){
for(auto i : edges){
cout<<i.first<<" -> ";
for(auto j: i.second){
cout<<j<<", ";
}
cout<<endl;
}
}
};
int main(){
Graph<int> graph;
int inputFirstEdge=0;
int inputSecondEdge=0;
bool directedOrUndirected;
while(inputFirstEdge>-1){
cin>>inputFirstEdge;
if(inputFirstEdge>-1){
cin>>inputSecondEdge;
cin>>directedOrUndirected;
graph.insert(inputFirstEdge, inputSecondEdge, directedOrUndirected);
}
}
graph.print();
return 0;
}