2.有一个整数数组a,设计一个算法,将所有偶数位的元素移到奇数位的元素的前面,要求它们的相对次序不变。 样例:a={1,2,3,4,5,6,7,8},移动后变为a={2,4,6,8,1,3,5,7}

#include
#define Maxsize 50
using namespace std;

template
class SqQueue
{
public:
T *data;
int front,rear;
SqQueue()
{
data=new T[Maxsize];
front=0,rear=0;
}
~SqQueue()
{
delete [] data;
}
bool empty()
{
return rearfront;
}
bool Max()//判满
{
return (rear+1+Maxsize)%Maxsize
front;
}
bool push(T e)
{
if(Max())
return false;
rear++;
data[rear]=e;
return true;
}
bool pop(T &e)
{
if(empty())
return false;
front++;
e=data[front];
return true;
}
void Display()//输出函数
{
int x;
while(!empty())
{
pop(x);
cout<<x<<" ";
}
cout<<endl;
}

void ReQueue(SqQueue st,SqQueue sp)//核心函数
{
int e;
while(!empty())
{
pop(e);
if(front%2==0)
{
st.push(e);//偶数
}
else
{
sp.push(e);//奇数
}

   }
   while(!st.empty()||!sp.empty())
   {
       while(!st.empty())
       {
           st.pop(e);
           push(e);
       }
       while(!sp.empty())
       {
           sp.pop(e);
           push(e);
       }
   }

}
};

int main()
{
SqQueue sq,st,sp;
int i;
cout<<“建立队列:”;
while(cin>>i)
{
sq.push(i);
if(cin.get()==’\n’)
{
break;
}
}
sq.ReQueue(st,sp);
cout<<“重建后的队列:”;
sq.Display();

return 0;

}

  • 1
    点赞
  • 13
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值