#include<iostream>
using namespace std;
#define MAXSIZE 100
typedef struct node
{
int *elem;
int length;
}List;
void creatList(List &L)
{
L.elem=new int[MAXSIZE];
L.length=0;
return;
}
void inputList(List &L,int n)
{
int i=0;
for(i;i<n;i++)
{
cout<<"请输入第"<<i+1<<"个数据:";
cin>>L.elem[i];
}
L.length=i;
return;
}
void output(List &L)
{
cout<<"顺序表为:";
int i=0;
for(i;i<L.length;i++)
{
cout<<L.elem[i]<<" ";
}
cout<<endl;
return;
}
void MergeList(List LA,List LB,List &LC)
{
LC.length=LA.length+LB.length; //新表LC长度为A,B两表长度之和
LC.elem=new int[LC.length]; //为合并后的新表LC 分配一个新的空间
int i=0,j=0,k=0;
while(i<LA.length && j<LB.length)
{
if(LA.elem[i]<=LB.elem[j])
{
LC.elem[k]=LA.elem[i];
i++;
k++;
}
else
{
LC.elem[k]=LB.elem[j];
j++;
k++;
}
}
while(i<LA.length)
{
LC.elem[k]=LA.elem[i];
i++;
k++;
}
while(j<LB.length)
{
LC.elem[k]=LB.elem[j];
j++;
k++;
}
return;
}
int main()
{
List LA;
List LB;
List LC;
int a;
int b;
cout<<"表 A 填充的数据个数为:";
cin>>a;
cin.get();
cout<<"表 B 填充的数据个数为:";
cin>>b;
creatList(LA);
creatList(LB);
cout<<"表 A 的输入为:"<<endl;
inputList(LA,a);
cout<<"表 B 的输入为:"<<endl;
inputList(LB,b);
cout<<"表 A 的展示如下:"<<endl;
output(LA);
cout<<"表 B 的展示如下:"<<endl;
output(LB);
cout<<"对表A和表B进行合并."<<endl;
MergeList(LA,LB,LC);
cout<<"合并后的表展示如下:"<<endl;
output(LC);
return 0;
}
本代码采用的是C++进行编译,对有顺序的两个表进行合并。代码中LA,LB表需要进行自行输入,你可以根据已知有顺序数据进行顺序输入,顺序为由小到大,即得有顺序的表LA,LB,再进行合并得到LC。