-
Notifications
You must be signed in to change notification settings - Fork 0
/
graph_AdjacencyList.cpp
63 lines (52 loc) · 1.06 KB
/
graph_AdjacencyList.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<vector>
using namespace std;
class node{
public:
int vertex;
int weight;
node(int v, int w){
vertex = v;
weight = w;
}
};
class Graph{
private:
int vertex;
int edge;
vector<node>*adjList;
public:
Graph(int vertex){
this->vertex = vertex;
this->adjList = new vector<node> [vertex];
}
bool addEdge(int u,int v,int weight)
{
if(u >= this->vertex || v >= this->vertex){
return false;
}
adjList[u].push_back(node(v,weight));
adjList[v].push_back(node(u,weight));
this->edge++;
return true;
}
void printGraph()
{
for(int i = 0; i< vertex; i++){
for(node element : adjList[i]){
cout << "(" << element.vertex << "," << element.weight << ")";
}
cout << "\n";
}
}
};
int main(){
Graph g(4);
g.addEdge(0,1,5);
g.addEdge(1,3,2);
g.addEdge(0,2,1);
g.addEdge(2,3,5);
//g.removeEdge(2,3);
g.printGraph();
return 0;
}