#include<stdio.h>
#include<stdlib.h>
int windows[7] = { 0 };
int number = 0;
typedef struct QNode
{
int data;
struct QNode *next;
}QNode;
typedef struct LinkQueue
{
QNode *front;
QNode *rear;
}LinkQueue;
int initQueue(LinkQueue &Q)
{
Q.front = Q.rear = (QNode*)malloc(sizeof(QNode));
if (!Q.front)
{
return -1;
}
Q.front->next = NULL;
return 0;
}
int enQueue(LinkQueue &Q, int data)
{
QNode *p = (QNode*)malloc(sizeof(QNode));
if (!p)
{
return -1;
}
p->data = data;
p->next = NULL;
Q.rear->next = p;
Q.rear = p;
return 0;
}
int deQueue(LinkQueue &Q,int &data)
{
if (Q.front == Q.rear)
{
return -1;
}
QNode *p = Q.front->next;
data = p->data;
Q.front->next = p->next;
if (p == Q.rear)
{
Q.rear = Q.front;
free(p);
}
return 0;
}
int isEmpty(LinkQueue &Q)
{
if (Q.front == Q.rear)
{
return 1;
}
return 0;
}
void print(LinkQueue *Q)
{
QNode *p;
p = Q->front->next;
while (p)
{
printf("%d ", p->data);
p = p->next;
}
}
void entryBank(LinkQueue &gjj, LinkQueue &yhk, LinkQueue &lck, LinkQueue &customer1, LinkQueue &customer2, LinkQueue &customer3)
{
int i,data;
printf("请输入要办理的业务:\n1.公积金\n2.银行卡\n3.理财卡\n");
scanf("%d", &i);
number++;
switch (i)
{
case 1:
if (!isEmpty(gjj))
{
deQueue(gjj,data);
windows[data] = 1;
printf("%d号顾客到%d号柜台办理\n", number, data);
}
else
{
enQueue(customer1,number);
printf("%d号顾客到队列1等待\n", number);
}
break;
case 2:
if (!isEmpty(yhk))
{
deQueue(yhk, data);
windows[data] = 1;
printf("%d号顾客到%d号柜台办理\n", number, data);
}
else
{
enQueue(customer2, number);
printf("%d号顾客到队列2等待\n", number);
}
break;
case 3:
if (!isEmpty(lck))
{
deQueue(lck, data);
windows[data] = 1;
printf("%d号顾客到%d号柜台办理\n", number, data);
}
else if(!isEmpty(yhk))
{
deQueue(yhk, data);
windows[data] = 1;
printf("%d号顾客到%d号柜台办理\n", number, data);
}
else
{
enQueue(customer3, number);
printf("%d号顾客到队列3等待\n", number);
}
default:
break;
}
}
void leaveBank(LinkQueue &gjj, LinkQueue &yhk, LinkQueue &lck, LinkQueue &customer1, LinkQueue &customer2, LinkQueue &customer3)
{
int i,number;
printf("请输入办理完成的窗口(1-6)\n");
scanf("%d", &i);
switch (i)
{
case 1:
if (!isEmpty(customer1))
{
deQueue(customer1, number);
printf("%d号顾客到%d号柜台办理\n", number, i);
}
else
{
enQueue(gjj, i);
windows[i] = 0;
}
break;
case 2:
case 3:
case 4:
if (!isEmpty(customer2))
{
deQueue(customer2, number);
printf("%d号顾客到%d号柜台办理\n", number, i);
}
else if (!isEmpty(customer3))
{
deQueue(customer3, number);
printf("%d号顾客到%d号柜台办理\n", number, i);
}
else
{
enQueue(yhk, i);
windows[i] = 0;
}
break;
case 5:
case 6:
if (!isEmpty(customer3))
{
deQueue(customer3, number);
printf("%d号顾客到%d号柜台办理\n", number, i);
}
else
{
enQueue(lck, i);
windows[i] = 0;
}
break;
default:
break;
}
}
void startSystem(LinkQueue &gjj, LinkQueue &yhk, LinkQueue &lck, LinkQueue &customer1, LinkQueue &customer2, LinkQueue &customer3)
{
int i;
printf("请输入顾客行为:\n1.进入\n2.离开\n3.查看窗口状态\n");
scanf("%d", &i);
switch (i)
{
case 1:
entryBank(gjj, yhk, lck, customer1, customer2, customer3);
break;
case 2:
leaveBank(gjj, yhk, lck, customer1, customer2, customer3);
break;
case 3:
for (int j = 1; j < 7; j++)
{
windows[j] ? printf("%d号窗口有人\n", j) : printf("%d号窗口无人\n", j);
}
break;
default:
break;
}
}
int main()
{
LinkQueue gjj, yhk, lck;
LinkQueue customer1, customer2, customer3;
initQueue(gjj);
enQueue(gjj, 1);
initQueue(yhk);
enQueue(yhk, 2);
enQueue(yhk, 3);
enQueue(yhk, 4);
initQueue(lck);
enQueue(lck, 5);
enQueue(lck, 6);
initQueue(customer1);
initQueue(customer2);
initQueue(customer3);
while (true)
{
startSystem(gjj, yhk, lck, customer1, customer2, customer3);
}
}