[说明及要求] 病人到医院看病,需要排队等候,先到先看。请编写程序模拟病人看病的过程。①后到的病人必须排在最后面;②排队过程中的病人可以随时查看自己前面还有多少病人等候看病;③系统能提示正在看病的后面一个病人做好准备。 [提示] (1) 定义结构体表示病人信息(姓名、性别、年龄)。 (2) 使用单链表实现。 (3) 分别定义函数实现在单链表的后面插入一个元素,删除单链表的第一个元素,查找单链表的任何一个元素(按姓名查找)等功能。 (4) 在main函数中进行模拟。
时间: 2023-03-19 11:21:49 浏览: 70
以下是一个简单的程序,用于模拟病人看病的过程:
```python
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
// 定义病人结构体
struct patient {
char name[20];
char gender;
int age;
struct patient *next;
};
// 向单链表中插入一个元素
void insert_patient(struct patient **head, char *name, char gender, int age) {
struct patient *new_patient = (struct patient*)malloc(sizeof(struct patient));
strcpy(new_patient->name, name);
new_patient->gender = gender;
new_patient->age = age;
new_patient->next = NULL;
if (*head == NULL) {
*head = new_patient;
} else {
struct patient *p = *head;
while (p->next != NULL) {
p = p->next;
}
p->next = new_patient;
}
}
// 删除单链表的第一个元素
void delete_patient(struct patient **head) {
if (*head == NULL) {
return;
}
struct patient *temp = *head;
*head = (*head)->next;
free(temp);
}
// 按照姓名查找单链表的元素
struct patient *find_patient(struct patient *head, char *name) {
struct patient *p = head;
while (p != NULL) {
if (strcmp(p->name, name) == 0) {
return p;
}
p = p->next;
}
return NULL;
}
// 输出当前排队的病人信息
void print_patients(struct patient *head) {
struct patient *p = head;
printf("当前排队的病人信息如下:\n");
while (p != NULL) {
printf("姓名:%s,性别:%c,年龄:%d\n", p->name, p->gender, p->age);
p = p->next;
}
}
int main() {
struct patient *head = NULL;
int num = 0;
char name[20];
char gender;
int age;
while (1) {
printf("请输入您的姓名(输入exit退出):");
scanf("%s", name);
if (strcmp(name, "exit") == 0) {
break;
}
printf("请输入您的性别(m表示男性,f表示女性):");
scanf(" %c", &gender);
printf("请输入您的年龄:");
scanf("%d", &age);
insert_patient(&head, name, gender, age);
num++;
printf("您前面有 %d 个人在排队等候\n", num-1);
print_patients(head);
}
printf("现在轮到您了,请进入医生办公室进行就诊\n");
while (head != NULL) {
printf("下一位病人是:%s\n", head->name);
printf("医生准备好了,请进入医生办公室进行就诊\n");
delete_patient(&head);
}
printf("排队结束,欢迎下次再来就诊\n");
return 0;
}
```
这个程序使用了单链表来存储排队的病人信息。
相关推荐
![](https://img-home.csdnimg.cn/images/20210720083646.png)
![docx](https://img-home.csdnimg.cn/images/20210720083331.png)
![doc](https://img-home.csdnimg.cn/images/20210720083327.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)