//*********************************** |
//功能:A、B元素为整数,严格递增,相同存入C,不同存入D,另分配空间 |
//日期:2017年9月19日 |
//作者:Ryan19 |
//*********************************** |
#include <iostream> |
using namespace std; |
const int ListlnitSize=0; |
const int Listlnc=10; |
typedef int LElemType; |
struct SList |
{ |
LElemType *elem; |
int length,listsize; |
}; |
bool Listlnit(SList &L); //顺序表初始化 |
bool ListCreate(SList &L, int n,LElemType a[]); //创建顺序表 |
bool Classify(SList &A,SList &B,SList &C,SList &D); //新表 |
int main() |
{ |
const int m=5,n=3; |
LElemType a[m]={1,2,3,4,5}; |
LElemType b[n]={2,4,6}; |
SList A,B,C,D; |
ListCreate(A,m,a); |
ListCreate(B,n,b); |
cout<< "线性表A为" <<endl; |
for ( int j=0;j<m;j++){cout<<A.elem[j]<< " " ;} |
cout<<endl<< "线性表B为" <<endl; |
for ( int i=0;i<n;i++){cout<<B.elem[i]<< " " ;} |
Classify(A,B,C,D); |
cout<<endl<< "重新排列后的顺序表C为" <<endl; |
for ( int k=0;k<C.length;k++){cout<<C.elem[k]<< " " ;} |
cout<<endl<< "重新排列后的顺序表D为" <<endl; |
for ( int t=0;t<D.length;t++){cout<<D.elem[t]<< " " ;} |
cout<<endl; |
return 0; |
} |
bool Listlnit(SList &L) |
{ |
L.elem= new LElemType[ListlnitSize]; |
if (!L.elem) return false ; |
L.length=0; |
L.listsize=ListlnitSize; |
return true ; |
} |
bool ListCreate(SList &L, int n,LElemType a[]) |
{ |
int i; |
L.elem= new LElemType[n+ListlnitSize]; |
if (!L.elem) return false ; |
L.length=n; |
L.listsize=n+ListlnitSize; |
for (i=0;i<n;i++) |
{ |
L.elem[i]=a[i]; |
} |
return true ; |
} |
bool Classify(SList &A,SList &B,SList &C,SList &D) |
{ |
if (A.length<=B.length) |
{ |
C.elem= new LElemType[A.length+ListlnitSize]; |
D.elem= new LElemType[B.length+ListlnitSize]; |
C.listsize=A.length+ListlnitSize; |
D.listsize=B.length+ListlnitSize; |
} |
else |
{ |
C.elem= new LElemType[B.length+ListlnitSize]; |
D.elem= new LElemType[A.length+ListlnitSize]; |
C.listsize=B.length+ListlnitSize; |
D.listsize=A.length+ListlnitSize; |
} |
C.length=0; |
D.length=0; |
int k=0,t=0,i=0,j=0,m; |
for (m=0;i<A.length && j<B.length;m++) |
{ |
if (A.elem[i]==B.elem[j]) |
{ |
C.elem[k]=A.elem[i]; |
k++; |
i++; |
j++; |
} |
else if (A.elem[i]<B.elem[j]) |
{ |
D.elem[t]=A.elem[i]; |
t++; |
i++; |
} |
else |
{ |
D.elem[t]=B.elem[j]; |
t++; |
j++; |
} |
} |
while (i<A.length) |
{ |
D.elem[t]=A.elem[i]; |
t++; |
i++; |
} |
while (j<B.length) |
{ |
D.elem[t]=B.elem[j]; |
t++; |
j++; |
} |
C.length=k; |
D.length=t; |
return true ; |
} |