I am new at the graph section. I recently tried to write program which will take number of vertices , edges and the connections between vertices.
#include<iostream>
#include<queue>
#include<vector>
using namespace std ;
void add_edge(vector<int> adj[] , int v1 , int v2 )
{
adj[v1].push_back(v2);
adj[v2].push_back(v1);
}
void print_graph ( vector<int>adj[] , int vertices)
{
for(int i = 0 ; i < vertices ; i++)
{
cout<< "adjacency list of vertex " << i << " is : \n" ;
cout<< "head -> " ;
for(auto it = adj[i].begin() ; it != adj[i].end() ; it++)
{
cout << *it << " -> " ;
}
cout<< "\n" ;
}
}
int main()
{
int ver , ed , i , j , s , v1 , v2 , temp ;
cin >> ver >> ed ;
vector<int> adj[ver] ;
for(i = 0 ; i < ver ; i++)
{
cin>> v1 ;
for(i = 0 ; i < ed ; i++)
{
cin >> v2 ;
char ch = (char)v2 ;
if (v2 == '\n')
{
break ;
}
add_edge(adj , v1 , v2 ) ;
}
}
print_graph(adj , ver);
return 0 ;
}
I was confused about how to stop the loop when the user presses enter . So I tried this. Doesn't look so cool but logical in my opinion.
cin >> v2 ;
char ch = (char)v2 ;
if (v2 == '\n')
{
break ;
}
But the complete program is not working properly. Please help me to figure out.