La=LaULb (循环链表)

时间:2023-06-09 18:05:20
#include<stdio.h>
typedef struct LNode
{
int data;
struct LNode *next;
}LNode,*LinkList;
void union1(LinkList &La,LinkList &Lb)
{
LinkList qb;
LinkList pa=La->next->next;
LinkList pb=Lb->next->next;
LinkList rc=La->next;
while(pa!=La->next&&pb!=Lb->next)
{
if(pa->data<pb->data)
{
rc->next=pa;
rc=pa;
pa=pa->next;
}
else if(pa->data>pb->data)
{
rc->next=pb;
rc=pb;
pb=pb->next;
}
else
{
rc->next=pa;
rc=pa;
pa=pa->next;
qb=pb;
pb=pb->next;
delete qb;
}
}
if(pb==Lb->next)
rc->next=pa;
else
{
LinkList t = La->next ;
rc->next=pb;
pb=Lb->next;
Lb->next= t ;
La=Lb;
}
// 该种方法也可以 else{ Lb->next=La->next;rc->next=pb;La=Lb;}
delete pb;
}
int main()
{
int m,n,i,a[100],b[100];
scanf("%d%d",&m,&n);
for(i=0;i<m;i++)
scanf("%d",&a[i]);
for(i=0;i<n;i++)
scanf("%d",&b[i]);
LinkList p=new LNode;
LinkList La=p;
for(i=0;i<m;i++)
{
LinkList s=new LNode;
s->data=a[i];
La->next=s;
La=s;
La->next=p;
}
LinkList q=new LNode;
LinkList Lb=q;
for(i=0;i<n;i++)
{
LinkList s=new LNode;
s->data=b[i];
Lb->next=s;
Lb=s;
Lb->next=q;
}
union1(La,Lb);
p=La->next->next;
while(p!=La->next)
{
printf("%d ",p->data);
p=p->next;
}
return 0;
}