// // main.cpp // 数据结构-实验二-银行实现 // // Created by loglian on 2020/5/2. // Copyright © 2020 loglian. All rights reserved. // #include <iostream> #define MAXSIZE 1000 typedef int Status; typedef int QElemType; typedef struct { QElemType *base; int front; int rear; }SqQueue; Status InitQueue(SqQueue &Q) //初始化 { //构造一个空队列Q Q.base=new QElemType[MAXSIZE]; if(!Q.base) exit(-2); Q.front=Q.rear=0; return 1; } Status EmptyQueue(SqQueue &Q) //空 { if (Q.front==Q.rear) return 1; else return 0; } Status FullQueue(SqQueue &Q) //满 { if ((Q.rear+1)%MAXSIZE==Q.front) return 1; else return 0; } Status EnQueue(SqQueue &Q,QElemType e) //入队 { if(FullQueue(Q)) return 0; Q.base[Q.rear]=e; Q.rear=(Q.rear+1)%MAXSIZE; return 1; } Status DeQueue(SqQueue &Q) //出队 { if(EmptyQueue(Q)) return 0; Q.front=(Q.front+1)%MAXSIZE; return 1; } Status pop(SqQueue &Q) { return Q.base[Q.front]; } int main() { int n,i,number; SqQueue A,B; InitQueue(A); InitQueue(B); scanf("%d",&n); for(i=0;i<n;i++) //入队AB { scanf("%d",&number); if(number%2) EnQueue(A, number); //奇数入A else EnQueue(B, number);//偶数入B } /*用于检验入队AB是否正确运行 for(i=0;i<n;i++) { while(!EmptyQueue(A)) { std::cout<<"A:"<<pop(A);DeQueue(A); } std::cout<<std::endl; while(!EmptyQueue(B)) { std::cout<<"B:"<<pop(B);DeQueue(B); } } */ while (!EmptyQueue(A)||!EmptyQueue(B)) { if(!EmptyQueue(A)) { std::cout<<pop(A); DeQueue(A); if(!EmptyQueue(A)) { std::cout<<" "<<pop(A); DeQueue(A); } if(!EmptyQueue(A)||!EmptyQueue(B)) std::cout<<" "; } if(!EmptyQueue(B)) { std::cout<<pop(B); DeQueue(B); if(!EmptyQueue(A)||!EmptyQueue(B)) std::cout<<" "; } } }
PTA:
输出截图: