//*********************************** |
//功能:改变顺序表的元素顺序,第奇数个元素移到前面,第偶数个元素移到后面 |
//日期:2017年9月13日 |
//作者:Ryan2019 |
//*********************************** |
#include <iostream> |
using namespace std; |
const int ListlnitSize=0; |
const int Listlnc=10; |
typedef int LElemType; |
struct SList |
{ |
LElemType *elem; |
int length,listsize; |
}; |
bool Listlnit(SList &L); //顺序表初始化 |
bool ListCreate(SList &L, int n,LElemType a[]); //创建顺序表 |
bool MoveOrder(SList &L); //改变顺序表的元素顺序 |
int main() |
{ |
const int i=7; |
LElemType a[i]={1,2,3,4,5,6,7}; |
SList L; |
ListCreate(L,i,a); |
cout<< "原顺序表为" <<endl; |
for ( int j=0;j<i;j++){cout<<L.elem[j]<< " " ;} |
MoveOrder(L); |
cout<<endl<< "改变元素后的顺序表为" <<endl; |
for (j=0;j<L.length;j++){cout<<L.elem[j]<< " " ;} |
cout<<endl; |
return 0; |
} |
bool Listlnit(SList &L) |
{ |
L.elem= new LElemType[ListlnitSize]; |
if (!L.elem) return false ; |
L.length=0; |
L.listsize=ListlnitSize; |
return true ; |
} |
bool ListCreate(SList &L, int n,LElemType a[]) |
{ |
int i; |
L.elem= new LElemType[n+ListlnitSize]; |
if (!L.elem) return false ; |
L.length=n; |
L.listsize=n+ListlnitSize; |
for (i=0;i<n;i++) |
{ |
L.elem[i]=a[i]; |
} |
return true ; |
} |
bool MoveOrder(SList &L) |
{ |
int i,j=0,m=0,n,p; |
SList T; |
T.elem= new LElemType[L.length+ListlnitSize]; |
T.length=0; |
T.listsize=L.length+ListlnitSize; |
for (i=0;i<L.length;i++) //把表L复制给T |
{ |
T.elem[j]= L.elem[i]; |
j++; |
T.length++; |
} |
for (n=0;n<T.length;n+=2) |
{ |
L.elem[m]=T.elem[n]; |
m++; |
} |
for (p=1;p<T.length;p+=2) |
{ |
L.elem[m]=T.elem[p]; |
m++; |
} |
delete []T.elem; |
return true ; |
} |