#include <bits/stdc++.h>
#define Max 1010000
using namespace std;
typedef int ElemType;
class List
{
public:
ElemType *elem;
int maxSize;
int size;
List() /*List类的无参构造函数*/
{
elem = (ElemType *)malloc(Max * sizeof(ElemType));
maxSize = Max;
size = 0;
}
void CreatList(int n) /*建表*/
{
int x;
for(int i = 0; i < n; i++)
{
cin >> x;
elem[size++] = x;
}
}
void MergeList(List a, List b)
{
int i,j;
size = 0;
i = 0;
j = 0;
while(i < a.size && j < b.size)
{
if(a.elem[i] < b.elem[j])
{
elem[size++] = a.elem[i];
i++;
}
else
{
elem[size++] = b.elem[j];
j++;
}
}
while(i < a.size)
{
elem[size++] = a.elem[i];
i++;
}
while(j < b.size)
{
elem[size++] = b.elem[j];
j++;
}
}
void OutputList() /*输出*/
{
for(int i = 0; i < size; i++)
i == size - 1? cout << elem[i] << endl : cout << elem[i] << " ";
}
void DestroyList() /*销毁顺序表*/
{
delete elem;
}
};
int main()
{
List a, b, c;
int n, m;
cin>>n>>m;
a.CreatList(n);
b.CreatList(m);
c.MergeList(a,b);
a.DestroyList();
b.DestroyList();
c.OutputList();
c.DestroyList();
return 0;
}
有序顺序表归并
最新推荐文章于 2020-12-26 23:14:44 发布