//单链表的合并
//链表合并
//两个链表必须是有序的
#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<Maxsize;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<Maxsize;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;
}
双链表&链表合并&多项式相加算法,布布扣,bubuko.com
原文地址:http://blog.csdn.net/wqthaha/article/details/38497841