题目描述
已知在一维数组A[m+n]中依次存放两个线性表(a1,a2,a3…,am)和(b1,b2,b3…,bn)。试编写一个函数,将数组中两个顺序表的位置互换,即将(b1,b2,b3…,bn)(a1,a2,a3…,am)的前面。
#include<iostream>
using namespace std;
#define Maxsize 1000
typedef struct
{
int data[Maxsize];
int length;
}sqList;
void reverse(sqList &L,int left,int right)
{
for (int i =left; i <=(right+left) / 2; i++)
{
int temp = L.data[i];
L.data[i] = L.data[right+left - i];
L.data[right+left - i] = temp;
}
}
int main()
{
sqList L = { 1,2,3,4,5,6,7,8,9,10 };
L.length = 10;
cout << "init seq:";
for (int i = 0; i < L.length; i++)
cout << L.data[i] << " ";
cout << endl;
reverse(L, 0, 4);
cout << "前n个逆置:";
for (int i = 0; i < L.length; i++)
cout << L.data[i] << " ";
cout << endl;
reverse(L, 5, 9);
cout << "后m个逆置:";
for (int i = 0; i < L.length; i++)
cout << L.data[i] << " ";
cout << endl;
cout << "整体逆置(即最终结果):";
reverse(L, 0, 9);
for (int i = 0; i < L.length; i++)
cout << L.data[i] << " ";
cout << endl;
return 0;
}