基于C#双向循环链表的医院数据管理:患者关联信息输出问题
医院数据管理应用:实现患者关联信息展示的改造方案
现有基于双向循环链表的医院数据管理代码,仅能查询并展示患者基本信息,无法关联显示患者对应的主治医生、用药记录及患病信息。以下是具体改造步骤及完整代码:
一、扩展双向循环链表的查询能力
原DoublyLinkedCircularList<T>类的QueryById仅能返回单个匹配项,无法满足药品这类一个患者对应多条记录的场景,需添加批量查询方法:
public IEnumerable<T> QueryAllById(Func<T, bool> query) { if (Head == null) yield break; Node<T> current = Head; do { if (query(current.ID!)) yield return current.ID!; current = current.Next!; } while (current != Head); }
二、调整主程序查询逻辑,关联展示信息
在找到目标患者后,通过患者ID在医生、药品、疾病链表中查询对应记录,并分模块输出:
修改Main方法中的查询结果处理部分:
if (queriedPatient != null) { Console.WriteLine($"=== 患者信息 ==="); Console.WriteLine($"ID: {queriedPatient.ID}, 姓名: {queriedPatient.FirstName} {queriedPatient.LastName}"); // 查询关联医生 Console.WriteLine("\n=== 主治医生 ==="); Doctor? assignedDoctor = doctorList.QueryById(doc => doc.ID == ID); if (assignedDoctor != null) Console.WriteLine(assignedDoctor.ToString()); else Console.WriteLine("未关联医生"); // 查询关联药品 Console.WriteLine("\n=== 用药记录 ==="); var patientMedicines = medicineList.QueryAllById(med => med.ID == ID); if (patientMedicines.Any()) foreach (var med in patientMedicines) Console.WriteLine(med.ToString()); else Console.WriteLine("无用药记录"); // 查询关联疾病 Console.WriteLine("\n=== 患病信息 ==="); Disease? patientDisease = diseaseList.QueryById(dis => dis.ID == ID); if (patientDisease != null) Console.WriteLine(patientDisease.ToString()); else Console.WriteLine("无患病记录"); } else { Console.WriteLine("未找到患者"); }
三、完整修改后代码
using System; using System.Collections.Generic; using System.Linq; // Patient class public class Patient { public long? ID { get; set; } public string FirstName { get; set; } public string LastName { get; set; } public Patient? Previous { get; set; } public Patient? Next { get; set; } public override string ToString() { return $"{ID}. {FirstName} {LastName}"; } } // Medicine class public class Medicine { public long? ID { get; set; } public string Name { get; set; } public Medicine? Previous { get; set; } public Medicine? Next { get; set; } public override string ToString() { return $"{ID}. {Name}"; } } // Doctor class public class Doctor { public long ID { get; set; } public string Name { get; set; } public string Specialty { get; set; } public Doctor? Previous { get; set; } public Doctor? Next { get; set; } public override string ToString() { return $"{ID}. {Name} ({Specialty})"; } } // Disease class public class Disease { public long ID { get; set; } public string Name { get; set; } public Disease? Previous { get; set; } public Disease? Next { get; set; } public override string ToString() { return $"{ID}. {Name}"; } } // Doubly linked circular list class public class Node<T> { public T? ID { get; set; } public Node<T>? Previous { get; set; } public Node<T>? Next { get; set; } } public class DoublyLinkedCircularList<T> { private Node<T>? Head; public void Add(T element) { Node<T> newNode = new Node<T> { ID = element }; if (Head == null) { Head = newNode; Head.Previous = Head; Head.Next = Head; } else { newNode.Previous = Head.Previous; newNode.Next = Head; Head.Previous!.Next = newNode; Head.Previous = newNode; } } public T? QueryById(Func<T, bool> query) { if (Head == null) return default; Node<T> current = Head; do { if (query(current.ID!)) return current.ID; current = current.Next!; } while (current != Head); return default; } // 新增批量查询方法,返回所有匹配项 public IEnumerable<T> QueryAllById(Func<T, bool> query) { if (Head == null) yield break; Node<T> current = Head; do { if (query(current.ID!)) yield return current.ID!; current = current.Next!; } while (current != Head); } } class Program { static void Main(string[] args) { // Doubly linked circular lists DoublyLinkedCircularList<Patient> patientList = new DoublyLinkedCircularList<Patient>(); DoublyLinkedCircularList<Medicine> medicineList = new DoublyLinkedCircularList<Medicine>(); DoublyLinkedCircularList<Doctor> doctorList = new DoublyLinkedCircularList<Doctor>(); DoublyLinkedCircularList<Disease> diseaseList = new DoublyLinkedCircularList<Disease>(); // Add sample data patientList.Add(new Patient { ID = 46573806740, FirstName = "Hiranur", LastName = "Sazak" }); patientList.Add(new Patient { ID = 23746982347, FirstName = "Nisa Nur", LastName = "Özdal" }); patientList.Add(new Patient { ID = 89374938243, FirstName = "Nisa Gül", LastName = "Ünal" }); patientList.Add(new Patient { ID = 98723424674, FirstName = "Berfin", LastName = "Geleş" }); medicineList.Add(new Medicine { ID = 46573806740, Name = "Theraflu Forte" }); medicineList.Add(new Medicine { ID = 46573806740, Name = "Ketober %1.6 Gargara" }); // ... (add more medicines) doctorList.Add(new Doctor { ID = 46573806740, Name = "Dr. Ömer Kaplan", Specialty = "Ear, Nose, Throat" }); doctorList.Add(new Doctor { ID = 23746982347, Name = "Dr. Ali Nazmican Güröz ", Specialty = "Orthopedics" }); // ... (add more doctors) diseaseList.Add(new Disease { ID = 46573806740, Name = "Cold" }); diseaseList.Add(new Disease { ID = 23746982347, Name = "Meniscus Tear" }); // ... (add more diseases) Console.WriteLine("\n...ID 查询..."); Console.Write("请输入ID编号: "); if (long.TryParse(Console.ReadLine(), out long ID)) { Patient? queriedPatient = patientList.QueryById(patient => patient.ID == ID); if (queriedPatient != null) { Console.WriteLine($"=== 患者信息 ==="); Console.WriteLine($"ID: {queriedPatient.ID}, 姓名: {queriedPatient.FirstName} {queriedPatient.LastName}"); // 查询关联医生 Console.WriteLine("\n=== 主治医生 ==="); Doctor? assignedDoctor = doctorList.QueryById(doc => doc.ID == ID); if (assignedDoctor != null) Console.WriteLine(assignedDoctor.ToString()); else Console.WriteLine("未关联医生"); // 查询关联药品 Console.WriteLine("\n=== 用药记录 ==="); var patientMedicines = medicineList.QueryAllById(med => med.ID == ID); if (patientMedicines.Any()) foreach (var med in patientMedicines) Console.WriteLine(med.ToString()); else Console.WriteLine("无用药记录"); // 查询关联疾病 Console.WriteLine("\n=== 患病信息 ==="); Disease? patientDisease = diseaseList.QueryById(dis => dis.ID == ID); if (patientDisease != null) Console.WriteLine(patientDisease.ToString()); else Console.WriteLine("无患病记录"); } else { Console.WriteLine("未找到患者"); } } else { Console.WriteLine("输入的ID编号无效"); } Console.ReadKey(); } }
改造说明
- 新增
QueryAllById方法,支持从链表中获取所有匹配指定条件的记录,适配一个患者对应多条药品记录的场景。 - 调整主程序的查询输出逻辑,在获取患者信息后,通过相同ID关联查询医生、药品、疾病数据,并分模块展示。
- 界面输出优化为中文,更符合使用习惯。
内容的提问来源于stack exchange,提问作者Meral Hamarat
相关产品推荐
相关产品推荐

