DS顺序表--合并操作
程序员文章站
2022-05-08 09:15:53
...
#include<iostream>
using namespace std;
// 顺序表类定义
class seqlist
{
private:
int *list;
int maxsize;
int size;
public:
seqlist()
{
maxsize = 1000;
size = 0;
list = new int [maxsize];
}
~seqlist()
{
delete []list;
}
void creat(int *p, int size_num)
{
int i;
size = size_num;
for(i = 0; i < size; i++)
{
list[i] = p[i];
}
}
int list_size()
{
return size;
}
void display()
{
int i;
cout << list_size()<< " ";
for(i =0 ; i < size; i++)
cout << list[i] << " ";
cout << endl;
}
void mix(const seqlist &r1, const seqlist &r2)
{
int i, j = 0, k = 0;
int size_p = r2.size;
size = r1.size + r2.size;
int *p = new int [r2.size];
for(i = 0; i < r2.size; i++)
p[i] = r2.list[i];
for(i = 0; i < r1.size; i++)
{
if(r1.list[i] < p[k])
{
list[j] = r1.list[i];
j++;
continue;
}
else
{
list[j] = p[k];
j++;
k++;
i--;
size_p--;
}
if(size_p == 0)
{
i++;
for(; i < r1.size; i++, j++)
{
list[j] = r1.list[i];
}
break;
}
}
if(size_p != 0)
{
for(i = 0; i < size_p; i++, j++)
{
list[j] = p[i+k];
}
}
}
};
int main()
{
int size, i;
cin >> size;
seqlist a;
int *arr1 = new int [size];
for(i = 0; i < size; i++)
{
cin >> arr1[i];
}
a.creat(arr1, size);
delete []arr1;
cin >> size;
seqlist b;
int *arr2 = new int [size];
for(i = 0; i < size; i++)
{
cin >> arr2[i];
}
b.creat(arr2, size);
delete []arr2;
seqlist c;
c.mix(a,b);
c.display();
return 0;
}
样例输入
3 11 33 55
5 22 44 66 88 99
样例输出
8 11 22 33 44 55 66 88 99