Problem Description
Input
第二行依次输入M个有序的整数;
第三行依次输入N个有序的整数。
Output
Example Input
6 5 1 23 26 45 66 99 14 21 28 50 100
Example Output
1 14 21 23 26 28 45 50 66 99 100
#include <stdio.h>
#include <stdlib.h>
struct node
{
int data;
struct node *next;
}*head1,*tail,*head2,*p,*q;
int main()
{
int m,n,i,j,k;
scanf("%d %d",&m,&n);
head1=(struct node *)malloc(sizeof(struct node));
head1->next=NULL;
tail=head1;
for(i=1;i<=m;i++)
{
p=(struct node *)malloc(sizeof(struct node));
p->next=NULL;
scanf("%d",&p->data);
tail->next=p;
tail=p;
}
p=head1->next;
head2=(struct node *)malloc(sizeof(struct node));
head2->next=NULL;
tail=head2;
for(i=1;i<=n;i++)
{
q=(struct node *)malloc(sizeof(struct node));
q->next=NULL;
scanf("%d",&q->data);
tail->next=q;
tail=q;
}
q=head2->next;
head1->next=NULL;
tail=head1;
free(head2);
while(p&&q)
{
if(p->data<q->data)
{
tail->next=p;
tail=p;
p=p->next;
tail->next=NULL;
}
else
{
tail->next=q;
tail=q;
q=q->next;
tail->next=NULL;
}
if(p)
{
tail->next=p;
}
else
{
tail->next=q;
}
}
p=head1->next;
for(i=1;i<=m+n;i++)
{
if(i!=m+n)
{
printf("%d ",p->data);
}
else
printf("%d\n",p->data);
p=p->next;
}
return 0;
}