Note that there are some explanatory texts on larger screens.

plurals
  1. POWhat is wrong with this insertion sort using linked list code in c?
    text
    copied!<p>I am getting error while executing this. Compiler does not give any error but when executed it give random output.</p> <p>What am i doing is taking input from user &amp; storing them to linked list &amp; then implementing insertion sort. ( I am able to call the subroutine insertion sort, so I guess the problem lies within subroutine only)</p> <pre><code>#include&lt;stdio.h&gt; #include&lt;stdlib.h&gt; struct node { int info; struct node *next; }; typedef struct node *NODEPTR; NODEPTR getnode(); NODEPTR insertionSortLinkedList(NODEPTR head); int main() { int i,n,temp; NODEPTR head,lptr,prevlptr; printf("No of input integers to be sorted\n"); scanf("%d",&amp;n); if (n&lt;2){printf("n should be atleast 2 \n");return 0;} printf("\nType space-separated array of %d integers\n",n); scanf("%d",&amp;temp); head=getnode(); head-&gt;info=temp; head-&gt;next=NULL; prevlptr=head; for (i=0;i&lt;n-1;i++) { scanf("%d",&amp;temp); lptr=getnode(); lptr-&gt;info=temp; prevlptr-&gt;next=lptr; lptr-&gt;next=NULL; prevlptr=lptr; } head=insertionSortLinkedList(head); lptr=head; while(lptr!=NULL) { printf("%d ",lptr-&gt;info); prevlptr=lptr; lptr=lptr-&gt;next; free(prevlptr); } return 0; } NODEPTR getnode() { NODEPTR p; p=(struct node*)malloc(sizeof(struct node)); return p; } NODEPTR insertionSort(NODEPTR head) { NODEPTR listptr,tempptr,prevptr,prevtempptr; prevptr=head; listptr=prevptr-&gt;next; while(listptr!=NULL) { while (listptr-&gt;info &lt; prevptr-&gt;info) { prevptr-&gt;next=listptr-&gt;next; tempptr=head; prevtempptr=head; while(tempptr-&gt;info &lt;= listptr-&gt;info) { prevtempptr=tempptr; tempptr=tempptr-&gt;next; } if(tempptr-&gt;info == prevtempptr-&gt;info) { listptr-&gt;next=head; head=listptr; } else {e listptr-&gt;next=prevtempptr-&gt;next; prevtempptr-&gt;next=listptr; } listptr=prevptr-&gt;next; if (listptr==NULL) break; } prevptr=prevptr-&gt;next; if (prevptr=NULL) break; listptr=prevptr-&gt;next; } return(head); } </code></pre> <p>What is wrong with my code and how do I fix it?</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload