everytime I run this loop , I get a segmentation fault in the second iteration of the loop .
node *new,*new1;
new=(node*) malloc(sizeof(node));
new1=(node*) malloc(sizeof(node));
new = start->next;
for(;new->next != NULL;new = new->next)
{
for(new1=new->next;new1 != NULL;new1=new1->next)
{ //printf("LOOP\n");
if(new->data > new1->data)
{
printf("\n Swapping - new:%d and new1:%d\n",new->data,new1->data);
temp = (node*) malloc(sizeof(node));
temp1 = (node*) malloc(sizeof(node));
temp1 = new->next;
temp = new1->next;
new->next = temp;
printf("Temp var : %d\n",temp->data);
printf("Temp1 var : %d\n",temp1->data);
new1->next = new;
new1 = new;
new = temp1;
printf("After swapping , new:%d and new1 : %d\n",new->data,new1->data);
// free(temp);
// free(temp1);
}
}
}
Whenever I give a list to it - eg. 4,1,9,6 it only swaps 4 and 1 , when it is the iteration to swap 9 and 6 , it shows and segmentation fault .