//MergeList_L.cpp
//This program is to merge two sorted LNode into one
# include <stdlib.h>
# include <iostream.h>
# include <conio.h>
#include<stdio.h>
# define INIT_LENGTH 10
typedef struct LNode //define LNode structure
{ int data;
struct LNode *next;
}LNode,*LinkList;
void MergeList_L(LinkList &La,LinkList &Lb,LinkList &Lc) //MergeList_L()
{ LNode *pa,*pb,*pc;
pa=La->next;
pb=Lb->next;
Lc=pc=La;
cout<<endl;
while(pa&&pb)
if(pa->data<=pb->data)
{ pc->next=pa;
pc=pa;
pa=pa->next;
}
else
{ pc->next=pb;
pc=pb;
pb=pb->next;
}
if (!pa) //the end of pb
{ pc->next=pb;
pc=pc->next;
}
else //the end of pa
{ pc=pa;
pc=pc->next;
}
} //MergeList_L() end
void CreateList_L(LinkList &L,int n) //CreateList_L() function
{ //To Creatre a LinkList L with HeadNode
int i;
LNode *p;
L=(LinkList)malloc(sizeof(LNode));
L->next=NULL;
for(i=n;i>0;--i)
{
p=(LinkList)malloc(sizeof(LNode));
cin>>p->data;
p->next=L->next;
L->next=p;
}//end of for
if(n) cout<<"Success to Create a LinkList !"<<endl;
else cout<<"A NULL LinkList have been created !"<<endl;
}//end of CreateList() function
void main() //main() function
{ LinkList La,Lb,Lc;
int na,nb,nc,j, *q;
cout<<"请输入表a的长度:";
cin>>na;
cout<<"Please input the data for La Nodes: <eg. 34,67,3,-9,45,...>"<<endl;
CreateList_L(La,na);
cout<<"请输入表b的长度:";
cin>>nb;
cout<<"Please input the data for Lb Nodes: <eg. 34,67,3,-9,45,...>"<<endl;
CreateList_L(Lb,nb);
printf("nc=%d\n",nc=na+nb);
int i;
LNode *p;
Lc=(LinkList)malloc(sizeof(LNode));
Lc->next=NULL;
for(i=na+nb;i>0;--i)
{
p=(LinkList)malloc(sizeof(LNode));
p->next=Lc->next;
Lc->next=p;
}//end of for
if(na+nb) cout<<"Success to Create a LinkList !"<<endl;
else cout<<"A NULL LinkList have been created !"<<endl;
MergeList_L(La,Lb,Lc);
cout<<"合并之后的线性表c为:";
for(j=1;j<=na+nb;q++)
{ *q=Lc->data;
cout<<*q;
cout<<endl;
}
cout<<endl<<endl<<"...OK!...";
getch();
} //main() end