问题:已知在以为数组A[m+n]中依次存放着两个线性表(a1,a2,a3,...,am),和(b1,b2,b3....bn)。试编写一个函数,将数组中两个人顺序表的位置互换,即将(b1,b2,b3...bn)放在(a1,a2,a3...,am)的前面。
算法思想:
先将数组A[m+n]中的全部元素(a1,a2,a3...am,b1,b2,b3.....bn)逆置成(bn......b3,b2,b1am....a3,a2,a1)
接着对前n个元素和后m个元素分别使用逆置算法,就可以的到(b1,b2,b3....bn,a1,a2,a3...am)从而实现顺序表的位置互换、
代码实现
#include<stdio.h>
#include<malloc.h>
#define maxSize 10
typedef int DataType;
void Reverse(DataType A[],int left,int right,int arraySize)
{
if(left>=right||right>=arraySize)
return;
int mid=(right+left)/2;
for(int i=0;i<mid-left;i++){
DataType temp=A[left+i];
A[left+i]=A[right-i];
A[right-i]=temp;
}
}
void exchange(DataType A[],int m,int n,int arraySize)
{
Reverse(A,0,m+n-1,arraySiz