title: linked list insertion sort in c
body: <p>the program should do the insertion ascending sort for the nodes,first it should check the names and if the names are equal it should sort the ids,i do not know what is the issue that does not sort properly.</p>
  
  <pre><code># include &lt;stdio.h&gt;
  # include &lt;string.h&gt;
  # include &lt;stdlib.h&gt;
  
  typedef struct nd{
  int id;
  char name[20];
  float gpa;
  struct nd *next;
  }node;
  
  
  typedef node *list;
  
  //---------------------------------------------------------------
  
  int insertlist(list *head,char *buffer)
  {
  list p,q,n;
  int m,num,k,sc;
  p=(list)malloc(sizeof(node));
  num=sscanf(buffer,"%d %s %f",&amp;(p-&gt;id),(p-&gt;name),(&amp;p-&gt;gpa));
  if(num!=3)
   {
    printf("info not complete\n");
    free(p);
    return 1;
   }
  else
  {
  
     if(!*head)
     {
      *head=p;
      p-&gt;next = NULL;
     }
  //******** sorting tthe names and ids for equal names
     else if(sc=strcmp((*head)-&gt;name,p-&gt;name)&gt; 0 || ((sc == 0) &amp;&amp; ((*head)-&gt;id &gt; p-&gt;id)))
        {//head is modified
          p-&gt;next=*head;
          *head=p;
        }
  
     else{
          n=*head;
          q=n-&gt;next;
          while(q &amp;&amp; ((sc=strcmp(q-&gt;name,p-&gt;name)&lt;0) || ((sc == 0) &amp;&amp; (q-&gt;id &lt; p-&gt;id))))
            {
             n=q;
             q=q-&gt;next;
            }
         n-&gt;next=p;
         p-&gt;next=q;
         }
  }
  
  return 0;
  }
  
  //------------------------------------------------------
  
  int main()
  {
  int id,r;
  list head,p;
  FILE *fp;
  char c,buffer[100],filename[10];
  if ((fp=fopen("student.txt","r"))==NULL)
  {
    printf("error opening %s",filename);
    exit(1);
  }
  else
   {
  head=NULL;
  
  while(fgets(buffer,100,fp)!=NULL)
     {
      buffer[strlen(buffer)-1]=='\0';
      r=insertlist(&amp;head,buffer);
     }
  fclose(fp);
   }
  
  for(p=head;p!=NULL;p=p-&gt;next)
    printf("%d  %s  %f\n\n",p-&gt;id,p-&gt;name,p-&gt;gpa);
  }
  </code></pre>
  
  <p>An example of the contents of <code>student.txt</code>:</p>
  
  <pre><code>121513 ala 45.00
  121510 wang 21.00 
  145852 frank 26.00 
  151515 ala 25.00 
  </code></pre>