顺序表中存放两个子表,将其位置互换。

        已知在一维数组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;	
} 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

萌新在此~~

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值