两线性表元素按原顺序合并
#include<iostream.h>#include <stdlib.h>
#define maxsize 20
int n;
typedef struct
{int * data;
int length;
int listsize;
}sqlist;
void createsqlist(sqlist &list)
{list.data=(int*)malloc(sizeof(int)*maxsize);
if(!list.data)
exit(1);
list.length=0;
list.listsize=maxsize;
}
void initsqlist(sqlist &list)
{int i;
int * p;
cout<<"plese enta the number of data"<<endl;
cin>>n;
p=list.data;
cout<<"please enta your data in order"<<endl;
for(i=0;i<n;i++,p++)
{cin>>*p;
list.length++;}
cout<<"your list is"<<endl;
p=list.data;
for(i=0;i<n;i++,p++)
cout<<*p<<"\t";
cout<<endl;
}
void mergelist(sqlist &La,sqlist &Lb)
{sqlist Lc;
int *p1,*p2,*p3,*p1_last,*p2_last;
p1=La.data;
p2=Lb.data;
Lc.listsize=Lc.length=La.length+Lb.length;
Lc.data=(int*)malloc(sizeof(int)*(Lc.listsize));
p3=Lc.data;
p1_last=p1+La.length-1;
p2_last=p2+Lb.length-1;
if(!Lc.data)
exit(1);
while(p1<=p1_last&&p2<=p2_last)
{if(*p1<=*p2)
*p3++=*p1++;
else
*p3++=*p2++;
}
while(p1<=p1_last)
*p3++=*p1++;
while(p2<=p2_last)
*p3++=*p2++;
cout<<"combining has finished"<<endl;
cout<<"your new list is:"<<endl;
p3=Lc.data;
for(int i=0;i<Lc.length;i++,p3++)
cout<<*p3<<"\t";
cout<<endl;
}
int main()
{sqlist La,Lb;
createsqlist(La);
initsqlist(La);
createsqlist(Lb);
initsqlist(Lb);
mergelist(La,Lb);
return 0;
}