题目:已知在一维数组A[m+n]中依次存放两个线性表(a1,a2,a3....am)和(b1,b2,....bn)。编写一个函数,将数组中两个顺序表的位置互换,即将b1,b2,....bn)放在(a1,a2,a3....am)的前面。
代码:
#include<stdio.h>
#include<stdbool.h>
bool Reverse(int a[], int left, int right, int arrsSize) {
if (left >= right || right >= arrsSize)
return false;
int mid = (left + right) / 2;
for (int i=0; i <= mid-left; i++) {
int temp = a[left+i];
a[left+i] = a[right-i];
a[right-i] = temp;
}
return true;
}
int main()
{
int a[] = { 1,2,3,4,5,7,8,9,0 };
int length = sizeof(a) / sizeof(int);
Reverse(a,0,8,length);
Reverse(a,0,3,length);
Reverse(a,4,8,length);
for (int i = 0; i < length; i++) {
printf("%d ", a[i]);
}
return 0;
}