使用C语言利用队列实现银行叫号系统。给我详细代码谢谢
时间: 2024-05-11 12:15:17 浏览: 89
cc,c语言银行排队叫号系统源码,c语言程序
您好,我是ChitGPT。以下是使用C语言利用队列实现银行叫号系统的基本代码:
```c
#include <stdio.h>
#include <stdlib.h>
#define MAX_QUEUE_SIZE 100
typedef struct {
int number;
int window;
} Customer;
typedef struct {
Customer data[MAX_QUEUE_SIZE];
int front;
int rear;
} Queue;
void init(Queue *q) {
q->front = q->rear = 0;
}
int is_empty(Queue *q) {
return q->front == q->rear;
}
int is_full(Queue *q) {
return (q->rear + 1) % MAX_QUEUE_SIZE == q->front;
}
void enqueue(Queue *q, Customer c) {
if (is_full(q)) {
printf("The queue is full.");
exit(1);
}
q->data[q->rear] = c;
q->rear = (q->rear + 1) % MAX_QUEUE_SIZE;
}
Customer dequeue(Queue *q) {
if (is_empty(q)) {
printf("The queue is empty.");
exit(1);
}
Customer c = q->data[q->front];
q->front = (q->front + 1) % MAX_QUEUE_SIZE;
return c;
}
int main() {
Queue q;
init(&q);
int number = 0, window = 0;
while (1) {
printf("Please input customer number and window number (0 0 to exit): ");
scanf("%d %d", &number, &window);
if (number == 0 && window == 0) {
printf("Exit!");
break;
}
Customer c = {number, window};
enqueue(&q, c);
}
while (!is_empty(&q)) {
Customer c = dequeue(&q);
printf("Customer %d go to window %d.\n", c.number, c.window);
}
return 0;
}
```
以上代码通过定义一个Customer结构体来存储顾客的编号和窗口号,定义一个Queue结构体来实现队列,实现了队列的基本操作,包括初始化、判断队列是否为空或满、入队和出队操作。在主函数中,实现了输入顾客编号和窗口号的过程,并将其加入队列;最后输出每个顾客去的窗口号。
阅读全文