-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinit.cpp
66 lines (62 loc) · 1.25 KB
/
init.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
64
65
66
/*reference - geeksforgeeks*/
#include<iostream>
using namespace std;
struct node
{
int data;
node *next;
node(int el)
{
data = el;
next = NULL;
}
};
class AdjList
{
struct node **arr;
int V;
public:
AdjList(int size)
{
V = size;
arr = new struct node* [V];
for(int i = 0;i<V;i++)
arr[i] = NULL;
}
void addEdge(int a, int b)
{
struct node *newNode = new node(b);
newNode->next = arr[a];
arr[a] = newNode;
/*If undirected graph*/
struct node* newNode2 = new node(a);
newNode2->next = arr[b];
arr[b] = newNode2;
}
void printList()
{
for(int i = 0;i<V;i++)
{
struct node *temp = arr[i];
while(temp)
{
cout<<temp->data<<' ';
temp = temp->next;
}
cout<<endl;
}
}
};
int main()
{
AdjList G(5);
G.addEdge(0,1);
G.addEdge(0,4);
G.addEdge(1,2);
G.addEdge(1,3);
G.addEdge(1,4);
G.addEdge(2,3);
G.addEdge(3,4);
G.printList();
return 0;
}