Linked lists if youve ever had to visit a hospital

Info icon This preview shows pages 336–338. Sign up to view the full content.

View Full Document Right Arrow Icon
Linked Lists If you’ve ever had to visit a hospital emergency room, you’ll know that waiting in a queue is one of the defining features of the experience unless you were either very lucky or very unlucky. If you were lucky, the queue will have been empty and you will not have had to wait. Alternatively, if you were unlucky, your condition may have been sufficiently perilous that you got to jump to the head of the queue. In medical emergencies, a triage system will be in place to work out where each arriving patient should go in the queue. A similar pattern crops up in other scenarios—frequent fliers with gold cards may be allocated standby seats at the last minute even though others have been waiting for hours; celebrities might be able to walk right into a res- taurant for which the rest of us have to book a table weeks in advance. The LinkList<T> class is able to model these sorts of scenarios. At its simplest, you could use it like a Queue<T> —call AddLast to add an item to the back of the queue (as Enqueue would), and RemoveFirst to take the item off the head of the queue (like Dequeue would). But you can also add an item to the front of the queue with AddFirst . Or you can add items anywhere you like in the queue with the AddBefore and AddAfter methods. Example 9-13 uses this to place new patients into the queue. Example 9-13. Triage in action private LinkedList<Patient> waitingPatients = new LinkedList<Patient>(); ... LinkedListNode<Patient> current = waitingPatients.First; 312 | Chapter 9: Collection Classes
Image of page 336

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
while (current != null) { if (current.Value.AtImminentRiskOfDeath) { current = current.Next; } else { break; } } if (current == null) { waitingPatients.AddLast(newPatient); } else { waitingPatients.AddBefore(current, newPatient); } This code adds the new patient after all those patients in the queue whose lives appear to be at immediate risk, but ahead of all other patients—the patient is presumably either quite unwell or a generous hospital benefactor. (Real triage is a little more complex, of course, but you still insert items into the list in the same way, no matter how you go about choosing the insertion point.) Note the use of LinkedListNode<T> —this is how LinkedList<T> presents the queue’s contents. It allows us not only to see the item in the queue, but also to navigate back and forth through the queue with the Next and Previous properties. Stacks Whereas Queue<T> operates a FIFO order, Stack<T> operates a last in, first out (LIFO) order. Looking at this from a queuing perspective, it seems like the height of unfairness—latecomers get priority over those who arrived early. However, there are some situations in which this topsy-turvy ordering can make sense. A performance characteristic of most computers is that they tend to be able to work faster with data they’ve processed recently than with data they’ve not touched lately.
Image of page 337
Image of page 338
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

What students are saying

  • Left Quote Icon

    As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

    Student Picture

    Kiran Temple University Fox School of Business ‘17, Course Hero Intern

  • Left Quote Icon

    I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

    Student Picture

    Dana University of Pennsylvania ‘17, Course Hero Intern

  • Left Quote Icon

    The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

    Student Picture

    Jill Tulane University ‘16, Course Hero Intern