[c++]代码库
//***********************************
//功能:把两个递增的顺序表归并为递减的顺序表,对新表另分配空间
//日期:2017年9月19日
//作者:Ryan2019
//***********************************
#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 Descend(SList &A,SList &B,SList &C);//新表
int main()
{
const int m=3,n=4;
LElemType a[m]={1,3,5};
LElemType b[n]={2,4,6,8};
SList A,B,C;
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]<<" ";}
Descend(A,B,C);
cout<<endl<<"重新排列后的顺序表C为"<<endl;
for(int k=0;k<C.length;k++){cout<<C.elem[k]<<" ";}
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 Descend(SList &A,SList &B,SList &C)
{
C.elem=new LElemType[A.length+B.length+ListlnitSize];
C.listsize=A.length+B.length+ListlnitSize;
C.length=0;
int k=0,i=A.length-1,j=B.length-1,m;
for(m=0;m<A.length+B.length;m++)
{
if(A.elem[i]>=B.elem[j])
{
C.elem[k]=A.elem[i];
k++;
i--;
}
else
{
C.elem[k]=B.elem[j];
k++;
j--;
}
}
C.length=k;
return true;
}