//單鏈表的合並
//鏈表合並
//兩個鏈表必須是有序的
#define Maxsize 5
typedef int elemtype;
typedef struct linklist
{
elemtype data;
struct linklist *next;
}Linklist;
//建立鏈表1
Linklist *CreateList1 ()
{
int i,data ;
Linklist *head, *p, *q;
head=p=(Linklist *)malloc(sizeof(Linklist));
p->next=NULL; //創建單鏈表的表頭結點head
for(i=0;i
data =2*i;
q= (Linklist *)malloc(sizeof(Linklist));
q->data=data;
q->next=p->next;
p->next=q;
p=q;
}
return (head);
}
//建立鏈表2
Linklist *CreateList2 ()
{
int i,data ;
Linklist *head, *p, *q;
head=p=(Linklist *)malloc(sizeof(Linklist));
p->next=NULL; //創建單鏈表的表頭結點head
for(i=0;i
data =2*i+1; //減10,兩個鏈表不等
q= (Linklist *)malloc(sizeof(Linklist));
q->data=data;
q->next=p->next;
p->next=q;
p=q;
}
return (head);
}
int main()
{
linklist *La=CreateList1();
linklist *Lb=CreateList2();
linklist *Lc,*L1,*L2,*Lp;
Lc=(Linklist *)malloc(sizeof(Linklist));
Lc->next=NULL;
Lc->next=La->data < Lb->data ? La:Lb;
L1=La;
L2=Lb;
Lp=Lc;
while(L1->next!= NULL && L2->next!=NULL)
{
if(L1->data < L2->data )
{
Lp->next=L1;
L1=L1->next;
}
if(L1->data > L2->data )
{
Lp->next=L2;
L2=L2->next;
}
if(L1->data == L2->data )
{
Lp->next=L1;
L1=L1->next;
L2=L2->next;
}
}
if(L1->next= NULL)
Lp->next=L2;
else
Lp->next=L1;
while(1);
return 0;
}