-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAdjacency_list.cpp
More file actions
56 lines (44 loc) · 1.08 KB
/
Adjacency_list.cpp
File metadata and controls
56 lines (44 loc) · 1.08 KB
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 <bits/stdc++.h>
using namespace std;
class Node{
public:
string name;
list<string> nbrs;
Node(string name){
this->name = name;
}
};
class Graph{
unordered_map<string, Node*> umap;
public:
Graph(vector<string> cities){
for(auto city: cities){
umap[city] = new Node(city);
}
}
void addEdge(string a, string b, bool undir=false){
umap[a]->nbrs.push_back(b);
if (undir)
umap[b]->nbrs.push_back(a);
}
void printList(){
for(auto citypair: umap){
string city = citypair.first;
cout<<city<<"-->";
Node *nbr = citypair.second;
for(auto nbr: nbr->nbrs){
cout<<nbr<<",";
}
cout<<endl;
}
}
};
int main() {
vector<string> cities = {"delhi", "mumbai", "chennai", "kolkata"};
Graph g(cities);
g.addEdge("delhi", "mumbai");
g.addEdge("delhi", "chennai");
g.addEdge("chennai", "kolkata");
g.addEdge("mumbai", "kolkata");
g.printList();
}