#include"
stdio.h"
#include"malloc.h"
struct list
{
int data;
struct list *next;
};
struct list *head1,*head2,*p1,*p2,*q1,*q2;
void main()
{
int n=0;
void unionlist();
p1=q1=(struct list*)malloc(sizeof(struct list));
printf("请输入第一个
链表的信息\n");
scanf("%d",&p1->data);
while(p1->data!=0)
{
n=n+1;
if(n==1)
head1=q1;
else
q1->next=p1;
q1=p1;
p1=(struct list*)malloc(sizeof(struct list));
scanf("%d",&p1->data);
}
q1->next=NULL;
n=0;
p2=q2=(struct list*)malloc(sizeof(struct list));
printf("请输入第二个链表的信息\n");
scanf("%d",&p2->data);
while(p2->data!=0)
{
n=n+1;
if(n==1)
head2=q2;
else
q2->next=p2;
q2=p2;
p2=(struct list*)malloc(sizeof(struct list));
scanf("%d",&p2->data);
}
q2->next=NULL;
unionlist();
}
void unionlist()
{
struct list *head,*p;
int i=0;
p1=head1;
p2=head2;
head=p=(struct list*)malloc(sizeof(struct list));
p->data=0;
while(p1 && p2)
{
if(p1->data<=p2->data)
{
p->next=p1;
p=p1;
p1=p1->next;
}
else
{
p->next=p2;
p=p2;
p2=p2->next;
}
}
p->next=p1?p1:p2;
p=head;
printf("合并后的链表为如下:\n");
while(p)
{
i=i+1;
if(i==1)
p=p->next;
printf("%3d",p->data);
p=p->next;
}
printf("\n");
free(head1);
free(head2);
}