title: Creating Adjacency List
body: <p>I am having problem to create adjacent list in correct order. I think there is some problem in CreateAdjList(void)method. I run out of ideas. Please give me some tips. Basically I have graph and creating Adjacency list on connected edges.</p>
  
  <pre><code>#include &lt;stdio.h&gt;
  #include &lt;stdlib.h&gt;
  #define maxV 100                               
  typedef struct graphnode{                        
      int vertex;
      struct graphnode *next;
  }Node;
  Node **node;                             
  Node **nodeT; 
  
  FILE *fp;
  
  void initial(int nv);                            
  void AdjList(void);                              
  void PrintAdjList(int nv);                           
  
  
  int main()
  {
      int nv;
  
      fp= fopen("input.txt","r");
      fscanf(fp,"%d",&amp;nv);
      initial(nv);
      CreateAdjList();
      PrintAdjList(nv);
  
      return 0;
  }
  
  
  
  void initial(int nv)
  {
      int i;
      node = new Node *[maxV];
  
      for(i=1;i&lt;=nv;i++){
          node[i] = (Node *)malloc(sizeof(Node));
          node[i]-&gt;next=NULL;
  
  
      }
  
  }
  
  
  //CREATE ADJACENCY  LIST - 
  void CreateAdjList(void)
  {
      int v1,v2;
      Node *ptr;
  
  while(fscanf(fp,"%d%d",&amp;v1,&amp;v2)!=EOF){
  
          ptr = (Node *)malloc(sizeof(Node));
          ptr-&gt;vertex = v2;
          ptr-&gt;next = node[v1]-&gt;next;    //Problem could be here
          node[v1]-&gt;next = ptr;
  
      }
  
      fclose(fp);
  }
  
  
  
  
  //PRINT LIST
  void PrintAdjList(int nv)
  {
      int i;
      Node *ptr;
  
      for(i=1; i&lt;=nv; i++){
          ptr = node[i]-&gt;next;
          printf("    node[%2d]  ",i);
          while(ptr != NULL){
              printf("  --&gt;%2d", ptr-&gt;vertex);
              ptr=ptr-&gt;next;
          }
          printf("\n");
      }
      printf("\n");
  
  }
  </code></pre>
  
  <blockquote>
    <p>ACTUAL PROGRAM OUTPUT - WRONG ORDER . I attached output list in
    printed in revere way.</p>
  </blockquote>
  
  <p>Input:</p>
  
  <pre><code>8
  1 2
  2 3
  2 5
  2 6
  3 4
  3 7
  4 3
  4 8
  5 1
  5 6
  6 7
  7 6
  7 8
  8 8
  0 0
  
  Expected Output:
  Adjacency list represenation:
  1: 2 
  2: 3 5 6 
  3: 4 7 
  4: 3 8 
  5: 1 6 
  6: 7 
  7: 6 8 
  8: 8
  </code></pre>
  
  <blockquote>
    <p>My actual output is displayed wrong order. If you look at node the correct order should  be
    2 ->3->6->5</p>
  </blockquote>
  
  <pre><code> node[ 1]    --&gt; 2
   node[ 2]    --&gt; 6  --&gt; 5  --&gt; 3
   node[ 3]    --&gt; 7  --&gt; 4
   node[ 4]    --&gt; 8  --&gt; 3
   node[ 5]    --&gt; 6  --&gt; 1
   node[ 6]    --&gt; 7
   node[ 7]    --&gt; 8  --&gt; 6
   node[ 8]    --&gt; 8
  </code></pre>