医院排队系统C语言代码
时间: 2023-08-18 17:09:20 浏览: 62
以下是一个简单的使用C语言实现医院排队系统的示例代码:
```c
#include <stdio.h>
#include <stdlib.h>
#define MAX_QUEUE_SIZE 100
typedef struct {
int patient_id;
int priority; // 病人优先级,数字越小优先级越高
} Patient;
typedef struct {
Patient data[MAX_QUEUE_SIZE];
int front, rear;
} PriorityQueue;
void initQueue(PriorityQueue *queue) {
queue->front = queue->rear = 0;
}
int isQueueEmpty(PriorityQueue *queue) {
return queue->front == queue->rear;
}
int isQueueFull(PriorityQueue *queue) {
return (queue->rear + 1) % MAX_QUEUE_SIZE == queue->front;
}
void enqueue(PriorityQueue *queue, Patient patient) {
if (isQueueFull(queue)) {
printf("Queue is full.\n");
exit(1);
}
int i = queue->rear;
while (i != queue->front && patient.priority < queue->data[(i - 1 + MAX_QUEUE_SIZE) % MAX_QUEUE_SIZE].priority) {
queue->data[i] = queue->data[(i - 1 + MAX_QUEUE_SIZE) % MAX_QUEUE_SIZE];
i = (i - 1 + MAX_QUEUE_SIZE) % MAX_QUEUE_SIZE;
}
queue->data[i] = patient;
queue->rear = (queue->rear + 1) % MAX_QUEUE_SIZE;
}
Patient dequeue(PriorityQueue *queue) {
if (isQueueEmpty(queue)) {
printf("Queue is empty.\n");
exit(1);
}
Patient patient = queue->data[queue->front];
queue->front = (queue->front + 1) % MAX_QUEUE_SIZE;
return patient;
}
int main() {
PriorityQueue queue;
initQueue(&queue);
// 加入病人
Patient p1 = {1, 3};
Patient p2 = {2, 1};
Patient p3 = {3, 2};
enqueue(&queue, p1);
enqueue(&queue, p2);
enqueue(&queue, p3);
// 按优先级顺序取出病人
while (!isQueueEmpty(&queue)) {
Patient patient = dequeue(&queue);
printf("Patient %d is being treated (priority: %d).\n", patient.patient_id, patient.priority);
}
return 0;
}
```
该代码使用了优先队列(Priority Queue)数据结构来实现医院排队系统。每个病人都有一个优先级,数字越小优先级越高。在加入队列时,按照优先级的顺序插入队列中,插入时需要移动队列中的元素。在取出病人时,直接从队列头部取出元素即可。需要注意的是,该代码仅供参考,实际的医院排队系统需要考虑更多的因素和进行更复杂的实现。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_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)