用C语言使用链式存储实现队列,编写程序模拟患者入院看病的过程。【问题描述】其中,每个患者具有编号、姓名、病情等基本信息,可以按照病情优先级(如重症、中症、轻症)加入到医院的候诊队列中,然后依次进入医生门诊看病。当医生在看病时,其他患者需要等待,病情相同患者,根据编号排队等待。 【输入形式】编号 姓名 病情 【输出形式】编号 姓名 病情 【样例输入】1 张三 轻症 2 李四 重症 3 王五 中症 【样例输出】2 李四 重症 3 王五 中症 1 张三 轻症
时间: 2023-12-03 09:44:08 浏览: 26
以下是使用链式存储实现队列的C语言程序,实现患者入院看病的过程:
```c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
typedef struct patient {
int id; // 患者编号
char name[20]; // 患者姓名
char condition[10]; // 病情
struct patient *next;
} Patient;
typedef struct queue {
Patient *front; // 队头指针
Patient *rear; // 队尾指针
} Queue;
// 初始化队列
void initQueue(Queue *q) {
q->front = q->rear = NULL;
}
// 判断队列是否为空
int isQueueEmpty(Queue *q) {
return q->front == NULL;
}
// 入队操作
void enQueue(Queue *q, int id, char *name, char *condition) {
Patient *p = (Patient *)malloc(sizeof(Patient));
p->id = id;
strcpy(p->name, name);
strcpy(p->condition, condition);
p->next = NULL;
if (isQueueEmpty(q)) {
q->front = q->rear = p;
} else {
// 按照病情优先级插入队列
Patient *pre = NULL, *cur = q->front;
while (cur != NULL && strcmp(cur->condition, condition) <= 0) {
pre = cur;
cur = cur->next;
}
if (pre == NULL) {
p->next = q->front;
q->front = p;
} else if (cur == NULL) {
q->rear->next = p;
q->rear = p;
} else {
pre->next = p;
p->next = cur;
}
}
}
// 出队操作
void deQueue(Queue *q) {
if (isQueueEmpty(q)) {
printf("Queue is empty.\n");
} else {
Patient *p = q->front;
q->front = q->front->next;
printf("%d %s %s\n", p->id, p->name, p->condition);
free(p);
}
}
// 主函数
int main() {
Queue q;
initQueue(&q);
int id;
char name[20], condition[10];
// 输入患者信息并入队
while (scanf("%d %s %s", &id, name, condition) != EOF) {
enQueue(&q, id, name, condition);
}
// 按照病情优先级依次出队
while (!isQueueEmpty(&q)) {
deQueue(&q);
}
return 0;
}
```
使用样例输入运行程序,得到样例输出。