使用 C# 和双向链接循环列表管理医院数据:患者信息输出显示问题

问题描述 投票:0回答:1

此代码旨在使用双向链接循环列表创建医院数据管理应用程序。它可以分为两个主要部分:

课程: Patient、Medicine、Doctor 和Disease 类分别代表患者、药品、医生和疾病的基本数据结构。 每个类都包含 ID、名称、专业等属性,以及表示双向链接结构的节点引用(上一个和下一个)。

双向链接循环列表类: Node类表示双向链表的节点。 DoublyLinkedCircularList类根据数据类型管理双向链表。 Add 方法将一个新节点添加到列表的开头。 QueryById 方法根据给定 ID 在列表中搜索,如果找到则返回相应的数据。

主要程序: 在 Main 方法中,创建特定于患者、药物、医生和疾病类型的双向链接循环列表。 示例数据将添加到这些列表中。 系统会提示用户输入 ID,程序使用该 ID 查询患者信息。如果找到,则显示该信息;否则,显示“未找到患者”。留言。

一切正常,代码可以运行。但是为什么代码没有为我提供患者信息的输出? 如何在下面的代码中打印患者的医生、用药和疾病信息?

using System;
using System.Collections.Generic;

// 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;
    }
}

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 Query...");

        Console.Write("Please enter the ID number: ");
        if (long.TryParse(Console.ReadLine(), out long ID))
        {
            Patient? queriedPatient = patientList.QueryById(patient => patient.ID == ID);

            if (queriedPatient != null)
            {
                Console.WriteLine($"ID: {queriedPatient.ID}, First Name: {queriedPatient.FirstName}, Last Name: {queriedPatient.LastName}");
            }
            else
            {
                Console.WriteLine("Patient not found.");
            }
        }
        else
        {
            Console.WriteLine("Invalid ID number entered.");
        }
        Console.ReadKey();
    }
}
c# class data-structures doubly-linked-list
1个回答
0
投票

代码无法编译。如果删除多余的反斜杠,它将起作用。例如。

Node<T>?
代替
Node\<T\>?
string[]
代替
string\[\]

顺便说一句:循环列表对于此类数据没有任何意义。没有任何关于患者和医生的循环。

实施有待改进。数据类中不需要有

Previous
Next
属性。这些应该只属于
Node<T>
类。节点应该具有
Data
属性而不是
ID
属性:

public class Node<T>
{
    public required T Data { get; set; }
    public Node<T>? Previous { get; set; }
    public Node<T>? Next { get; set; }
}

向属性添加一些必需的修饰符可以删除空警告。

public class Patient
{
    public long ID { get; set; }
    public required string FirstName { get; set; }
    public required string LastName { get; set; }

    public override string ToString() => $"{ID}. {FirstName} {LastName}";
}

public class Medicine
{
    public long ID { get; set; }
    public required string Name { get; set; }

    public override string ToString() => $"{ID}. {Name}";
}

public class Doctor
{
    public long ID { get; set; }
    public required string Name { get; set; }
    public required string Specialty { get; set; }

    public override string ToString() => $"{ID}. {Name} ({Specialty})";
}

public class Disease
{
    public long ID { get; set; }
    public required string Name { get; set; }

    public override string ToString() => $"{ID}. {Name}";
}

将注释

// Medicine class
添加到
class Medicine
不会增加任何价值。注释应该添加代码中缺少的信息。例如。设计决策和其他不明显的事实。

public class DoublyLinkedCircularList<T>
{
    private Node<T>? _head;

    public void Add(T element)
    {
        var newNode = new Node<T> { Data = element };
        if (_head == null) {
            _head = new Node<T> { Data = element };
            _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!.Data)) {
                return current.Data;
            }
            current = current.Next;
        } while (current != _head);

        return default;
    }
}
© www.soinside.com 2019 - 2024. All rights reserved.