namespace WaitingRoom; /// /// A queue implementation which can be used to represents the waiting queue of /// patients at a doctor's office. /// public sealed class PatientQueue { private Node? _head; /// /// Gets the number of patients currently waiting in this queue /// public int Size { get; private set; } /// /// Adds a new patient to the queue. /// Patients are ordered by arrival time. /// /// Patient to add to the queue public void Enqueue(Patient patient) { var newNode = new Node(patient); if (_head == null || patient.Arrival < _head.Data.Arrival) { newNode.Next = _head; _head = newNode; Size++; return; } var current = _head; while (current.Next != null && current.Next.Data.Arrival <= patient.Arrival) { current = current.Next; } newNode.Next = current.Next; current.Next = newNode; Size++; } /// /// Removes the next patient from the queue. /// /// The next patient to be treated; null if no patient is waiting public Patient? Dequeue() { if (_head == null) { return null; } var patient = _head.Data; _head = _head.Next; Size--; return patient; } /// /// Removes the supplied patient from the queue. /// Only patients actually waiting in this queue can be removed. /// /// The patient to remove from the queue /// True if the patient could be removed; false otherwise public bool Remove(Patient patient) { if (_head == null) { return false; } if (_head.Data == patient) { _head = _head.Next; Size--; return true; } var current = _head; while (current.Next != null && current.Next.Data != patient) { current = current.Next; } if (current.Next == null) { return false; } current.Next = current.Next.Next; Size--; return true; } }