已知在一维数组A[m+n]中依次存放着两个顺序表(a1, a2, ..., am)和(b1, b2, ..., bn)。编写一个算法,将数组中两个顺序表的位置互换,即将(b1, b2, ..., bn)放在(a1, a2, ..., am)的前面。
#include <iostream>
#include <cstring>
#include <algorithm>
using namespace std;
#define N 40001
typedef int DataType;
typedef struct node
{
DataType a[N];
DataType lenth_1;
DataType lenth_2;
}linelist;
int main()
{
linelist L;
// 存储数据
cout << "请输入第一段顺序表的长度:";
cin >> L.lenth_1;
cout << "请输入第二段顺序表的长度:";
cin >> L.lenth_2;
cout << "请输入第一段的数据:";
for (int i = 0; i < L.lenth_1; i++) cin >> L.a[i];
cout << "请输入第二段的数据:";
for (int i = L.lenth_1; i < L.lenth_1 + L.lenth_2; i++) cin >> L.a[i];
// 交换两个顺序表的位置
reverse(L.a, L.a + L.lenth_1 + L.lenth_2); // 将所有数据逆转过来
reverse(L.a, L.a + L.lenth_2); // 将被逆转后的第二个表再将顺序调回
reverse(L.a + L.lenth_2, L.a + L.lenth_1 + L.lenth_2); // 将被逆转后的第一个表再将顺序调回
// 输出链表
for (int i = 0; i < L.lenth_2; i++) cout << L.a[i] << ' '; // 第二条顺序表的输出
cout << endl;
for (int i = L.lenth_2; i < L.lenth_1 + L.lenth_2; i++) cout << L.a[i] << ' '; // 第一条顺序表的输出
cout << endl;
return 0;
}