用C 语言栈和队列实现:【问题描述】其中,每个患者具有编号、姓名、病情等基本信息,可以按照病情优先级(如重症、中症、轻症)加入到医院的候诊队列中,然后依次进入医生门诊看病。当医生在看病时,其他患者需要等待,病情相同患者,根据编号排队等待。 【输入形式】编号 姓名 病情 【输出形式】编号 姓名 病情 【样例输入】1 张三 轻症 2 李四 重症 3 王五 中症 【样例输出】2 李四 重症 3 王五 中症 1 张三 轻症
时间: 2023-12-03 13:44:07 浏览: 27
以下是用 C 语言栈和队列实现该问题的代码:
```c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAX_NAME_LEN 20 // 姓名最大长度
#define MAX_PATIENTS_NUM 100 // 最大患者数量
// 患者结构体
typedef struct {
int id; // 编号
char name[MAX_NAME_LEN]; // 姓名
char severity[10]; // 病情
} Patient;
// 栈结构体
typedef struct {
int top; // 栈顶指针
Patient data[MAX_PATIENTS_NUM]; // 数据
} Stack;
// 队列结构体
typedef struct {
int front; // 队头指针
int rear; // 队尾指针
Patient data[MAX_PATIENTS_NUM]; // 数据
} Queue;
// 初始化栈
void initStack(Stack *s)
{
s->top = -1;
}
// 判断栈是否为空
int isStackEmpty(Stack *s)
{
return s->top == -1;
}
// 判断栈是否已满
int isStackFull(Stack *s)
{
return s->top == MAX_PATIENTS_NUM - 1;
}
// 入栈
void pushStack(Stack *s, Patient p)
{
if (isStackFull(s)) {
printf("Stack is full!\n");
return;
}
s->top++;
s->data[s->top] = p;
}
// 出栈
Patient popStack(Stack *s)
{
if (isStackEmpty(s)) {
printf("Stack is empty!\n");
exit(1);
}
Patient p = s->data[s->top];
s->top--;
return p;
}
// 初始化队列
void initQueue(Queue *q)
{
q->front = q->rear = -1;
}
// 判断队列是否为空
int isQueueEmpty(Queue *q)
{
return q->front == -1;
}
// 判断队列是否已满
int isQueueFull(Queue *q)
{
return q->rear == MAX_PATIENTS_NUM - 1;
}
// 入队
void enqueue(Queue *q, Patient p)
{
if (isQueueFull(q)) {
printf("Queue is full!\n");
return;
}
if (isQueueEmpty(q)) {
q->front = q->rear = 0;
} else {
q->rear++;
}
q->data[q->rear] = p;
}
// 出队
Patient dequeue(Queue *q)
{
if (isQueueEmpty(q)) {
printf("Queue is empty!\n");
exit(1);
}
Patient p = q->data[q->front];
if (q->front == q->rear) {
q->front = q->rear = -1;
} else {
q->front++;
}
return p;
}
// 根据病情判断优先级
int getPriority(char *severity)
{
if (strcmp(severity, "重症") == 0) {
return 2;
} else if (strcmp(severity, "中症") == 0) {
return 1;
} else {
return 0;
}
}
// 按照病情优先级入队
void enqueueByPriority(Queue *q, Patient p)
{
int priority = getPriority(p.severity);
if (priority == 0) {
enqueue(q, p); // 如果病情是轻症,直接入队
} else {
Stack s;
initStack(&s);
// 将队列中优先级大于等于当前患者的患者出队并放入栈中
while (!isQueueEmpty(q) && getPriority(q->data[q->front].severity) >= priority) {
pushStack(&s, dequeue(q));
}
// 将当前患者入队
enqueue(q, p);
// 将栈中的患者依次放回队列中
while (!isStackEmpty(&s)) {
enqueue(q, popStack(&s));
}
}
}
// 打印患者信息
void printPatient(Patient p)
{
printf("%d %s %s\n", p.id, p.name, p.severity);
}
int main()
{
Queue q;
initQueue(&q);
int n; // 患者数量
scanf("%d", &n);
for (int i = 0; i < n; i++) {
Patient p;
scanf("%d %s %s", &p.id, p.name, p.severity);
enqueueByPriority(&q, p);
}
// 依次出队并打印患者信息
while (!isQueueEmpty(&q)) {
printPatient(dequeue(&q));
}
return 0;
}
```
程序的核心是 `enqueueByPriority` 函数,它实现了按照病情优先级入队的功能。具体步骤如下:
1. 首先根据当前患者的病情获取优先级;
2. 如果当前患者是轻症,直接入队;
3. 如果当前患者是中症或重症,将队列中优先级大于等于当前患者的患者出队并放入栈中;
4. 将当前患者入队;
5. 将栈中的患者依次放回队列中。
这样就可以实现按照病情优先级加入候诊队列,并依次进入医生门诊看病的功能了。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.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)