-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGraph.cpp
63 lines (55 loc) · 1.1 KB
/
Graph.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
51
52
53
54
55
56
57
58
59
60
61
62
63
#include<iostream>
#include<list>
#include<bits/stdc++.h>
using namespace std;
class Graph {
public:
int v;
list<int> *adj;
Graph(int num) {
v = num;
adj = new list<int>[num];
}
void addEdge(int a, int b) {
adj[a].push_back(b);
adj[b].push_back(a);
}
void removeEdge(int a, int b) {
list<int>::iterator it = find(adj[a].begin(),adj[a].end(),b);
if(it != adj[a].end()) {
adj[a].erase(it);
}
list<int>::iterator it2 = find(adj[b].begin(),adj[b].end(),a);
if(it2 != adj[b].end()) {
adj[b].erase(it2);
}
}
bool isEdge(int a, int b) {
list<int>::iterator it = find(adj[a].begin(),adj[a].end(),b);
if(it != adj[a].end()) {
return true;
}
return false;
}
void printEdges(int x) {
list<int>::iterator it;
for(it=adj[x].begin(); it!=adj[x].end();++it) {
cout<<*it<<" ";
}
}
void printAdjList() {
for(int i=0;i<v;i++) {
printEdges(i);
cout<<endl;
}
}
};
int main() {
Graph g(5);
g.addEdge(0,4);
g.addEdge(1,3);
g.addEdge(0,1);
g.addEdge(3,2);
g.addEdge(2,4);
g.printAdjList();
}