I am trying to merge two already sorted linked lists temp1
and temp2
. I am getting segmentation fault for this example :
Input
temp1 : 1->NULL
temp2 : 2->3->4->5->6->NULL
Output
But if i remove "\n" from printf
statements then it directly prints segmentation fault.
Why this is happening?
Code :
list *merge(list * temp1, list * temp2)
{
list *x, *y, *z;
x = temp1;
y = temp2;
print(x);
print(y);
z = (list *) malloc(sizeof(list));
if (x->key < y->key)
{
z->key = temp1->key;
x = x->next;
}
else
{
z->key = temp2->key;
y = y->next;
}
z->next = NULL;
while (x || y)
{
printf("asdf\n");
if (x == NULL)
{
z = append(z, y->key);
y = y->next;
printf("x\n");
}
if (y == NULL)
{
z = append(z, x->key);
x = x->next;
printf("y\n");
}
if (x && y)
{
printf("x && y\n");
if (x->key < y->key)
{
z = append(z, x->key);
x = x->next;
}
else
{
z = append(z, y->key);
y = y->next;
}
}
}
print(z);
return z;
}
Thanks in advance.
I have already removed the segmentation fault. I am asking why it gets delayed when i remove the '\n' from printf statements?