代码:
#include <iostream>
using namespace std;
//判断是否为奇数
bool isOdd(int num)
{
return (num & 1) == 1;
}
void frontOddbackEven(int *pData, int length, bool(*fun)(int))
{
if (pData == nullptr || length <= 0)
return;
int* pFront = pData;
int* pBack = pData + length - 1;
while (pFront < pBack)
{
while (pFront < pBack && fun(*pFront))
++pFront;
while (pFront < pBack && !fun(*pBack))
--pBack;
if (pFront < pBack)
swap(*pFront, *pBack);
}
}
int main()
{
int data[] = { 1, 2, 3, 4, 5, 6, 7 };
int length = sizeof(data) / sizeof(int);
frontOddbackEven(data,length,isOdd);
for (int i = 0; i < length; i++)
cout << data[i] << " ";
cout << endl;
system("pause");
return 0;
}
测试: