-
Notifications
You must be signed in to change notification settings - Fork 0
/
direceted.cpp
55 lines (48 loc) · 1.11 KB
/
direceted.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
#include <iostream>
#include <list>
using namespace std;
struct Node{
int label;
list<int> neighbours;
};
struct Graph{
int n=9;
Node* nodes = new Node[n];
void intializenodes(){
for(int i=1;i<n;i++){
nodes[i].label = i;
}
}
void addedge(int u, int v){
nodes[u].neighbours.push_back(v);
}
void print(){
for(int i=0; i<n; i++){
cout << "Node " << nodes[i].label << ": ";
for(int neighbour : nodes[i].neighbours){
cout << neighbour << " ";
}
cout << endl;
}
}
};
int main() {
Graph* g = new Graph;
g->intializenodes();
//add edges for the graphs here.
g->addedge(1, 2);
g->addedge(1, 3);
g->addedge(1, 5);
g->addedge(1, 4);
g->addedge(2, 3);
g->addedge(2, 6);
g->addedge(4, 6);
g->addedge(4, 7);
g->addedge(4, 8);
g->addedge(5, 6);
g->addedge(5, 7);
g->addedge(5, 8);
//print the graph adjacency list
g->print();
delete g; // Free allocated memory
}