Advertisement
apl-mhd

graph Input using linkedlist

Apr 3rd, 2017
155
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.02 KB | None | 0 0
  1. #include <iostream>
  2. #include <cstdio>
  3.  
  4. using namespace std;
  5.  
  6. struct Edge{
  7.     int vertex;
  8.     struct Edge *next;
  9.    
  10. };
  11.  
  12. typedef struct Edge node;
  13.  
  14. node *Insert(node *currentHead, int newVertex){
  15.    
  16.     node *tempVertex;
  17.     tempVertex = new node();
  18.     tempVertex->vertex = newVertex;
  19.     tempVertex->next = currentHead;
  20.    
  21.     return tempVertex;
  22. }
  23.  
  24. int main(int argc, char **argv)
  25. {
  26.     int vertices, edges, i, v1, v2;
  27.    
  28.     cout<<"Number of vertices"<<endl;
  29.         cin>>vertices;
  30.     cout<<"Number of Edges"<<endl;
  31.         cin>>edges;
  32.    
  33.     node *adjanceyList[vertices+1] = {NULL};
  34.    
  35.    
  36.     for(i=0; i<edges; i++){
  37.        
  38.         cin>>v1>>v2;
  39.         adjanceyList[v1] = Insert(adjanceyList[v1], v2);
  40.         adjanceyList[v2] = Insert(adjanceyList[v2], v1);
  41.        
  42.        
  43.         }
  44.        
  45.     for(i=1; i<=vertices; i++){
  46.        
  47.         node *temp;
  48.        
  49.         temp = adjanceyList[i];
  50.         cout<<"Adjacencylist["<<i<<"]:";
  51.         while(temp !=NULL){
  52.            
  53.             printf("%d->", temp->vertex);
  54.             //cout<<temp->vertex;
  55.            
  56.             temp=temp->next;
  57.             }
  58.             cout<<"NULL"<<endl;
  59.        
  60.        
  61.         }
  62.    
  63.    
  64.    
  65.    
  66.     return 0;
  67. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement