设计算法程序:用链队实现病人看病(排队)模拟程序。
时间: 2024-06-10 14:06:01 浏览: 13
非常感谢您的提问。针对您的问题,以下是算法程序的实现:
首先定义一个病人队列,用链表来实现。每个链表节点包含病人的信息,如姓名、病症等等。为了模拟病人进入队列,我们可以提供一个输入函数,询问用户是否要添加新的病人。如果用户选择添加,则输入病人信息,并把病人加入到队列的末尾。
接下来,我们需要模拟医生看病的过程。我们可以使用一个变量来记录当前正在看病的病人,一个计时器来记录当前病人看病的时间。当计时器达到一定时间时,表示当前病人看完了病,可以从队列中移除,并将下一个病人放到队首开始看病。
当队列为空时,表示所有病人都看完了病,程序结束。
下面是具体的实现代码,供您参考:
```
#include <stdio.h>
#include <stdlib.h>
// 定义病人队列节点
typedef struct patient {
char name[20]; // 病人姓名
char symptom[50]; // 病症
struct patient *next;
} Patient;
// 定义病人队列
typedef struct queue {
Patient *front; // 队头
Patient *rear; // 队尾
} Queue;
// 初始化队列
void initQueue(Queue *queue) {
queue->front = queue->rear = NULL;
}
// 入队
void enQueue(Queue *queue, char name[], char symptom[]) {
Patient *newPatient = (Patient*) malloc(sizeof(Patient));
strcpy(newPatient->name, name);
strcpy(newPatient->symptom, symptom);
newPatient->next = NULL;
if (queue->rear == NULL) {
queue->front = queue->rear = newPatient;
} else {
queue->rear->next = newPatient;
queue->rear = newPatient;
}
}
// 出队
void deQueue(Queue *queue) {
if (queue->front == NULL) {
return;
}
Patient *patient = queue->front;
queue->front = queue->front->next;
free(patient);
if (queue->front == NULL) {
queue->rear = NULL;
}
}
// 获取队头元素
Patient* getFront(Queue *queue) {
return queue->front;
}
// 病人看病
void checkup(Patient *patient, int *time) {
printf("正在看病的病人是:%s,病症是:%s\n", patient->name, patient->symptom);
(*time)++; // 时间加1
}
int main() {
Queue queue;
initQueue(&queue); // 初始化队列
// 模拟添加病人
char name[20], symptom[50];
int num = 0;
while (num < 5) {
printf("是否要添加新的病人(1表示是,0表示否):");
int choice;
scanf("%d", &choice);
if (choice == 1) {
printf("请输入病人姓名和病症:");
scanf("%s%s", name, symptom);
enQueue(&queue, name, symptom); // 加入队列
num++;
} else {
break;
}
}
// 模拟看病过程
int time = 0; // 计时器
while (queue.front != NULL) {
Patient *patient = getFront(&queue);
checkup(patient, &time); // 看病
if (time >= 3) { // 病人看完病
printf("病人%s看完病了\n", patient->name);
deQueue(&queue); // 出队
time = 0; // 时间清零
}
}
return 0;
}
```
希望以上的回答能够对您有所帮助。
相关推荐
![rar](https://img-home.csdnimg.cn/images/20210720083606.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)