使用链式存储实现队列,编写程序模拟患者入院看病的过程。 【问题描述】其中,每个患者具有编号、姓名、病情等基本信息,可以按照病情优先级(如重症、中症、轻症)加入到医院的候诊队列中,然后依次进入医生门诊看病。当医生在看病时,其他患者需要等待,病情相同患者,根据编号排队等待。
时间: 2024-01-18 21:03:44 浏览: 31
以下是使用链式存储实现队列的代码,包括患者的信息、队列的数据结构以及入院看病的模拟过程:
```python
class Patient:
def __init__(self, id, name, condition):
self.id = id
self.name = name
self.condition = condition
class QueueNode:
def __init__(self, patient):
self.patient = patient
self.next = None
class Queue:
def __init__(self):
self.head = None
self.tail = None
def is_empty(self):
return self.head is None
def enqueue(self, patient):
node = QueueNode(patient)
if self.is_empty():
self.head = node
else:
self.tail.next = node
self.tail = node
def dequeue(self):
if self.is_empty():
return None
patient = self.head.patient
self.head = self.head.next
return patient
def peek(self):
if self.is_empty():
return None
return self.head.patient
def size(self):
count = 0
node = self.head
while node is not None:
count += 1
node = node.next
return count
def priority_sort(queue):
# 根据病情优先级和编号排序队列
if queue.is_empty():
return
node = queue.head
while node is not None:
next_node = node.next
if next_node is not None and \
(next_node.patient.condition > node.patient.condition or \
(next_node.patient.condition == node.patient.condition and next_node.patient.id < node.patient.id)):
# 如果下一个节点的病情优先级比当前节点高,或者病情相同但编号更小,则交换两个节点
temp = node.patient
node.patient = next_node.patient
next_node.patient = temp
node = node.next
# 创建一个空队列
queue = Queue()
# 患者入院看病
patient1 = Patient(1, "张三", "轻症")
patient2 = Patient(2, "李四", "重症")
patient3 = Patient(3, "王五", "中症")
patient4 = Patient(4, "赵六", "轻症")
patient5 = Patient(5, "钱七", "中症")
queue.enqueue(patient1)
queue.enqueue(patient2)
queue.enqueue(patient3)
queue.enqueue(patient4)
queue.enqueue(patient5)
# 根据病情优先级和编号排序队列
priority_sort(queue)
# 医生门诊看病
while not queue.is_empty():
patient = queue.dequeue()
print("患者 %s 正在看病" % patient.name)
```
其中,`Patient` 类表示患者的信息,包括编号、姓名和病情等基本信息;`QueueNode` 类表示队列的节点,包括患者信息和下一个节点的引用;`Queue` 类表示队列的数据结构,包括队列的头节点和尾节点。`priority_sort` 函数用于根据病情优先级和编号排序队列,其中如果下一个节点的病情优先级比当前节点高,或者病情相同但编号更小,则交换两个节点。最后,模拟医院的入院看病过程,将患者加入到队列中,并根据病情优先级和编号排序队列,然后依次出队进行医生门诊看病。
相关推荐
![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)