typedef struct node* Link;
struct node
{
int data;
struct node *next;
};
Link merge(Link s1,Link s2)
{
Link temp,s3;
s3=(Link)malloc(sizeof(struct node));
s3->data=0;
s3->next=NULL;
temp=s3;
while(s1&&s2)
{
if(s1->data<=s2->data)
{
temp->next=s1;
temp=temp->next;
s1=s1->next;
}
else
{
temp->next=s2;
temp=temp->next;
s2=s2->next;
}
}
if(s1)
temp->next=s1;
else if(s2)
temp->next=s2;
temp=s3;
s3=s3->next;
free(temp);
return s3;
}
Link creat()
{
Link h,s,r;
int x;
h=(Link)malloc(sizeof(struct node));
h->next =NULL;
h->data=0;
r=h;
scanf("%d",&x);
while(x!=-1)
{
s=(Link)malloc(sizeof(struct node));
s->data=x;
s->next=r->next;
r->next=s;
r=s;
scanf("%d",&x);
}
s=h;
h=h->next;
free(s);
return h;
}
int main()
{
Link s1,s2,s3;
int tag=1;
s1=creat();
s2=creat();
s3=merge(s1,s2);
if(s3==NULL)
printf("NULL\n");
while(s3->data!=-1)
{
if(tag)
{
tag=0;
printf("%d",s3->data);
}
else
printf(" %d",s3->data);
s3=s3->next;
}
return 0;
}
while(s3->data!=-1)变为while(s3)