-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGraph-LinkedList.cpp
More file actions
87 lines (82 loc) · 1.19 KB
/
Graph-LinkedList.cpp
File metadata and controls
87 lines (82 loc) · 1.19 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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
#include <bits/stdc++.h>
using namespace std;
struct Node
{
int n;
Node *next;
};
class LinkedList
{
public:
Node *L;
LinkedList()
{
L = NULL;
}
void addAtFront(int num)
{
if (L == NULL)
{
L = new Node;
L->n = num;
L->next = NULL;
}
else
{
Node *temp = new Node;
temp->n = num;
temp->next = L;
L = temp;
}
}
void print()
{
Node *temp;
temp = L;
while (temp != NULL)
{
cout << " [" << temp->n << "]";
temp = temp->next;
}
}
};
class Graph
{
public:
int inpVertices;
LinkedList *list;
Graph()
{
cout << "Enter Total Vertices: ";
cin >> inpVertices;
list = new LinkedList[inpVertices];
}
void addNewEdge(int x, int y)
{
list[x].addAtFront(y);
list[y].addAtFront(x);
}
void print()
{
cout << "\n Graph has " << inpVertices << " Vertices";
cout << "\n VERTICES->CONNECTED TO";
for (int i = 0; i < inpVertices; i++)
{
cout << " \n\t" << i << " -> ";
list[i].print();
}
}
};
int main(int argc, char **argv)
{
Graph obj;
obj.addNewEdge(0, 2);
obj.addNewEdge(2, 4);
obj.addNewEdge(2, 3);
obj.addNewEdge(1, 4);
obj.addNewEdge(4, 3);
obj.addNewEdge(2, 1);
obj.addNewEdge(3, 0);
obj.print();
return 0;
}