要求:
1。设银行有A、B两个业务窗口,A窗口的处理速度是B的2倍(A处理完两个顾客时,B处理完一个顾客)
2.奇数编号的顾客到A窗口办理业务,偶数编号的顾客到B窗口办理业务
当不同窗口同时处理完两个顾客时,A窗口顾客优先输出
代码:
//银行业务队列简单模拟
#include<stdio.h>
#include<stdlib.h>
#include<stdbool.h>
#define MaxQSize 1000
/*-----循环队列-----*/
typedef int ElementType;
typedef int Position;
typedef struct QNode *PtrToQNode;
struct QNode{
ElementType *Data;
Position Front,Rear;
int MaxSize;
};
typedef PtrToQNode Queue;
Queue CreateQueue(int MaxSize);
bool IsEmpty(Queue Q);
void AddQ(Queue Q,ElementType X);
ElementType DeleteQ(Queue Q);
/*-----队列定义结束-----*/
int main()
{
int N,Customer,i;
Queue A,B;
//初始化队列
A=CreateQueue(MaxQSize);
B=CreateQueue(MaxQSize);
printf("请输入顾客总数:");
scanf("%d",&N);
printf("请输入%d个顾客的编号:",N);
for(i=0;i<N;i++){
scanf("%d",&Customer);
if(Customer%2)AddQ(A,Customer);//奇数
else AddQ(B,Customer);//偶数
}
printf("客户处理队列:");
//输出第一个客户
if(!IsEmpty(A))
printf("%d ",DeleteQ(A));
else printf("%d ",DeleteQ(B));
while(!IsEmpty(A) && !IsEmpty(B)){
printf("%d ",DeleteQ(A));
printf("%d ",DeleteQ(B));
if(!IsEmpty(A))printf("%d ",DeleteQ(A));
}
while(!IsEmpty(A))//A不为空,B空
printf("%d ",DeleteQ(A));
while(!IsEmpty(B))//B不为空,A空
printf("%d ",DeleteQ(B));
printf("\n");
return 0;
}
Queue CreateQueue(int MaxSize){
Queue Q=(Queue)malloc(sizeof(struct QNode));
Q->Data =(ElementType *)malloc(sizeof(ElementType)*MaxSize);
Q->Front =Q->Rear =0;
Q->MaxSize =MaxSize;
return Q;
}
bool IsEmpty(Queue Q){
return (Q->Front ==Q->Rear) ;
}
void AddQ(Queue Q,ElementType X){
//简版入队,不检查队列满的问题
Q->Rear=(Q->Rear +1)%Q->MaxSize ;
Q->Data [Q->Rear ]=X;
}
ElementType DeleteQ(Queue Q){
Q->Front =(Q->Front +1)%Q->MaxSize ;
return Q->Data[Q->Front ];
}
运行:
![](https://i-blog.csdnimg.cn/blog_migrate/1f6de548d311fbfaa3c0332447802549.png)