SMASIF

Adjacency List

Jan 22nd, 2017
116
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.82 KB | None | 0 0
  1. #include<iostream>
  2. #include<cstdio>
  3. #include<cmath>
  4. #include<vector>
  5. #include<list>
  6.  
  7. using namespace std;
  8.  
  9. int main()
  10. {
  11.  
  12.     list<int> adj[100];
  13.     list<int>::iterator it;
  14.     int v,a,b,k=1,i;
  15.     cout<<"Input No Of Vertex: ";
  16.     cin>>v;
  17.     do
  18.     {
  19.         //cout<<"Input No Of Edges"<<endl;
  20.         cout<<"Edge "<<k<<endl;
  21.         cin>>a>>b;
  22.         if(a<=v && b<=v)
  23.         {
  24.            adj[a].push_back(b);
  25.            adj[b].push_back(a);
  26.            k++;
  27.         }
  28.  
  29.         else
  30.         {
  31.             cout<<"Invalid Input!!"<<endl;
  32.         }
  33.  
  34.  
  35.  
  36.     }while(a!=0 || b!=0);
  37.  
  38.     for(i=1;i<=v;i++)
  39.     {
  40.         printf("adj[%d]",i);
  41.         for(it=adj[i].begin();it!=adj[i].end();it++)
  42.         {
  43.             printf("-> %d",*it);
  44.         }
  45.  
  46.         cout<<endl;
  47.     }
  48.  
  49.     return 0;
  50.  
  51. }
Add Comment
Please, Sign In to add comment