银行业务队列简单模拟

要求:

1。设银行有A、B两个业务窗口,A窗口的处理速度是B的2倍(A处理完两个顾客时,B处理完一个顾客)

2.奇数编号的顾客到A窗口办理业务,偶数编号的顾客到B窗口办理业务

  1. 当不同窗口同时处理完两个顾客时,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 ];
}


运行:

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值