-sharpinclude<stdio.h>
-sharpinclude<stdlib.h>
-sharpinclude<time.h>
typedef struct node{
int num;
struct node *next;
} Node;
Node* merge(Node *a,Node *b) //
{
Node *l1=a,*l2=b;
Node *tmp=NULL;
Node *temp=NULL;
while(l1&&l2)
{
if(l1->num<=l2->num)
{
if(l1==a&&tmp==NULL) //
{
tmp=a;
temp=tmp;
}
else //
{
temp->next=l1;
temp=temp->next;
}
l1=l1->next;
}
else
{
if(l2==b&&tmp==NULL) //
{
tmp=b;
temp=tmp;
}
else //
{
temp->next=l2;
temp=temp->next;
}
l2=l2->next;
}
}
temp->next=(l1==NULL)?l2:l1;
return tmp;
}
Node *mergeSort(Node *a) //
{
Node *temp=a;
Node *tmp=temp;
if(temp==NULL||temp->next==NULL) return a;
while(temp&&temp->next)
{
temp=temp->next->next;
tmp=tmp->next;
}
temp=tmp->next;
tmp->next=NULL;
return merge(mergeSort(a),mergeSort(temp));
}
int main(void)
{
Node *a=(Node *)malloc(20*sizeof(Node));
srand(time(NULL));
for(int i=0;i<20;iPP)
{
(a+i)->num=rand()%50;
(a+i)->next=a+i+1;
}
a[19].next=NULL;
for(int i=0;i<20;iPP)
{
printf("%d ",a[i].num);
}
putchar("\n");
Node *l=mergeSort(a);
for(Node *b=l;b;b=b->next) //
{
printf("%d ",b->num);
}
putchar("\n");
return 0;
}
run this code and show an error. I hope warm-hearted people will help me point out the error.