70,037
社区成员
发帖
与我相关
我的任务
分享
#include<stdio.h>
#include<stdlib.h>
struct node
{
int data;
struct node *next;
};
int main()
{
int n,m,x,y,i,j;
scanf("%d %d",&m,&n);
struct node *head1,*head2,*q,*p,*t,*q1,*q2,*k;
head1=(struct node*)malloc(sizeof(struct node));
head1->next=NULL;
head2=(struct node*)malloc(sizeof(struct node));
head2->next=NULL;
q1 = head1;
q2 = head2;
for(i=1; i<=m; i++)
{
p = (struct node*)malloc(sizeof(struct node));
scanf("%d",&p->data);
p->next = NULL;
q1->next = p;
q1 = p;
}
for(i = 1; i<=n; i++)
{
p=(struct node*)malloc(sizeof(struct node));;
scanf("%d",&p->data);
p->next=NULL;
q2->next = p;
q2 = p;
}
q1 = head1->next;
q2 = head2->next;
t = head1;
while (q1 && q2) {
if (q1->data < q2->data) {
t->next = q1;
t = q1;
q1 = q1->next;
} else {
t->next = q2;
t = q2;
q2 = q2->next;
}
}
t->next = q1 ? q1 : q2;
p = head1->next;
while(p)
{
printf("%d ",p->data);
p = p->next;
}
/*
p = head2->next;
while(p)
{
k=p;
t=head1->next;
q=head1;
while(t)
{
if(k->data<t->data)
{
k->next=t;
q->next=k;
break;
}
t=t->next;
q=q->next;
}
if(t==NULL)
{
k->next=NULL;
q->next=k;
}
p=p->next;
}
p=head1->next;
while(p)
{
if(p->next==NULL)
printf("%d\n",p->data);
else
printf("%d ",p->data);
p=p->next;
}
*/
return 0;
}
参考一下吧
合并和输出可以对比参考一下;