#include<bits/stdc++.h>
using namespace std;
//函数状态码定义
#define TRUE 1
#define FALSE 0
#define OK 1
#define ERROR 0
#define INFEASIBLE -1
#define OVERFLOW -2
typedef int Status;
typedef int QElemType; //假设线性表中的元素均为整型
typedef struct QNode {
QElemType data;
struct QNode *next;
} QNode,*QueuePtr;
typedef struct{
QueuePtr front;
QueuePtr rear;
}LinkQueue;//链队列
Status InitQueue(LinkQueue &Q){
Q.front = Q.rear = (QNode*)malloc(sizeof(QNode));
if(!Q.front) exit(OVERFLOW);
Q.front->next = NULL;
return OK;
}
// Status DestroyQueue(LinkQueue &Q){
// QueuePtr p = Q.front, postp;
// while(p){
// postp = p->next;
// free(p);
// p = postp;
// }
// Q.front = NULL;Q.rear = NULL;
// return OK;
// }
Status EnQueue(LinkQueue &Q, QElemType e){
QueuePtr p = (QNode*)malloc(sizeof(QNode));
if(!p) exit(OVERFLOW);
p->data = e;
p->next = NULL;
Q.rear->next = p;
Q.rear = p;
return OK;
}
Status DeQueue(LinkQueue &Q, QElemType &e){
if(Q.front == Q.rear) return ERROR;//队列为空
QueuePtr p = Q.front->next; e = p->data;//取队首元素
Q.front->next = p->next;//指针后移
if(Q.rear == p) Q.rear = Q.front;//只剩一个元素的出队操作
return OK;
}
Status QEmpty(LinkQueue Q){
if(Q.front==Q.rear) return 1;
return 0;
}
int n, id;
LinkQueue A, B;
int main(){
InitQueue(A);
InitQueue(B);
scanf("%d",&n);
for(int i = 0; i < n; i++){
scanf("%d",&id);
if(id%2) EnQueue(A, id);
else EnQueue(B,id);
}
int cnt = 0, e;
while(!QEmpty(A) || !QEmpty(B)){
for(int i = 0; i < 2; i++){
if(!QEmpty(A)){
DeQueue(A, e);
if(cnt == 0){
printf("%d",e);
}
else printf(" %d",e);
cnt++;
}
}
if(!QEmpty(B)){
DeQueue(B, e);
if(cnt == 0) printf("%d",e);
else printf(" %d",e);
}
}
//system("pause");
return 0;
}
仅供参考 切勿抄袭
hang hang hang !!!