C语言队列实现:患者在医院看病过程:先排队等候,再看病治疗。 在排队的过程中主要重复做两件事情,一是患者达到诊室,将病历交给护士,排到等候队列中候诊;二是护士从等候队列中取出下一个患者的病历,该患者进入诊室看病。下班后,不再接受病人排队。按照“先到先服务”原则,设计一个算法模拟病人等候就诊的过程。 输入说明:“病人到达”通过控制台输入‘a’模拟,“护士让下一位患者就诊”通过控制台输入‘n’模拟,“不再接受病人排队” 通过控制台输入‘q’模拟。 输出说明:当有“病人到达”命令时,提示“病例号:”,输入一个整型数字后,输出“您的病例号是X,还有Y人在等候”;当有“护士让下一位患者就诊”命令时,“病例号为X的病人就诊”;当有“不再接收病人排队”命令时,则队列中所有元素出队,输出“今天看诊结束,下列病人依次就诊:X,Y,Z”,程序终止。
时间: 2023-12-26 14:07:15 浏览: 34
以下是基于C语言的队列实现,模拟患者排队就诊过程的代码:
```c
#include <stdio.h>
#include <stdlib.h>
#define MAXSIZE 50 // 队列最大长度
typedef struct {
int data[MAXSIZE];
int front, rear;
} Queue;
void initQueue(Queue *q) { // 初始化队列
q->front = q->rear = 0;
}
int isEmpty(Queue *q) { // 判断队列是否为空
return q->front == q->rear;
}
int isFull(Queue *q) { // 判断队列是否已满
return (q->rear + 1) % MAXSIZE == q->front;
}
int enQueue(Queue *q, int x) { // 入队
if (isFull(q)) {
printf("队列已满,不能再排队!\n");
return 0;
}
q->data[q->rear] = x;
q->rear = (q->rear + 1) % MAXSIZE;
return 1;
}
int deQueue(Queue *q) { // 出队
if (isEmpty(q)) {
printf("队列为空,没有病人等待就诊!\n");
return -1;
}
int x = q->data[q->front];
q->front = (q->front + 1) % MAXSIZE;
return x;
}
int main() {
Queue q;
initQueue(&q);
int patientNum = 0; // 病人数量
char cmd;
while (1) {
printf("请输入指令(a:病人到达,n:护士让下一位患者就诊,q:不再接收病人排队):");
scanf(" %c", &cmd);
if (cmd == 'a') { // 病人到达
patientNum++;
if (enQueue(&q, patientNum)) {
printf("您的病例号是%d,还有%d人在等候\n", patientNum, (q.rear - q.front + MAXSIZE) % MAXSIZE);
} else {
patientNum--;
}
} else if (cmd == 'n') { // 护士让下一位患者就诊
int x = deQueue(&q);
if (x != -1) {
printf("病例号为%d的病人就诊\n", x);
}
} else if (cmd == 'q') { // 不再接收病人排队
printf("今天看诊结束,下列病人依次就诊:");
while (!isEmpty(&q)) {
int x = deQueue(&q);
printf("%d ", x);
}
printf("\n");
break;
} else {
printf("无效指令,请重新输入!\n");
}
}
return 0;
}
```
运行程序后,按照提示输入指令即可模拟患者排队就诊的过程。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)