Skip to content

Commit f4733a4

Browse files
Adjacency List in Graph code added
1 parent 96b0857 commit f4733a4

File tree

1 file changed

+46
-0
lines changed

1 file changed

+46
-0
lines changed

Adjecency List .cpp

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
// Adjacency List in Graph
2+
3+
#include <bits/stdc++.h>
4+
using namespace std;
5+
6+
// Function for Adding Edges
7+
void addEdge(vector<int> adj[], int s, int d)
8+
{
9+
adj[s].push_back(d);
10+
adj[d].push_back(s);
11+
}
12+
13+
// Function for printing graph
14+
void printGraph(vector<int> adj[], int v)
15+
{
16+
for (int i = 0; i < v; i++)
17+
{
18+
cout << "Vertex " << i << ":";
19+
for (int x : adj[i])
20+
cout << "-> " << x;
21+
cout << endl;
22+
}
23+
}
24+
25+
// Main Function
26+
int main()
27+
{
28+
int V, s, d;
29+
cout << "How many Edges you want?: ";
30+
cin >> V;
31+
32+
// Create Graph
33+
vector<int> adj[V];
34+
35+
for (int i = 0; i < V-1; i++)
36+
{
37+
cout << "Enter Source: ";
38+
cin >> s;
39+
cout << "Enter Destination: ";
40+
cin >> d;
41+
addEdge(adj, s, d);
42+
cout << endl;
43+
}
44+
45+
printGraph(adj, V);
46+
}

0 commit comments

Comments
 (0)