Linked Lists:
Client Code Perspective

Alyce Brady
Kalamazoo College

Part 1: Adding Elements to a Linked List and Traversing a Linked List


News Bearers, Inc. is a courier service, and the owner, Ursula, wishes to keep track of her messengers.  She doesn't really care in what order they are stored or whether she has random access to all of them.  It is only important, for now, to be able to add new messengers to the list and print out all of them.

Getting Started


Part 2: Bad News Bearers*


There is some bad news to be delivered, and News Bearers, Inc. has taken on the dangerous mission.  Nobody really wants to be the one to take the news; the way goes through enemy territory and, even if the messenger gets through, the classic fate of the bearer of bad news may be waiting.  (Let's just say, this is how the phrase "Don't shoot the messenger" became relevant.)

To determine  which messenger will be sent, Ursula sits all of her messengers down in a circle, selects a number, and starts to count off.  Messengers are allowed to leave the circle one by one, and the last messenger left is the one who will deliver the bad news.  The counting off procedure is slightly unusual, however, because it is actually the messenger after the last one counted who gets to leave the circle. Consider the following example with 5 messengers, in which the number selected for counting off is 7.
 
 

We'll start at the "head" of the list and move forward.  Since it is circular, the "head" simply refers to whatever the current element in the list is.  We count off 7 messengers: Alyce, Autumn, Brad, Jon, Zach, Alyce, and Autumn.  Brad is declared "safe" and removed from the circle.
We then count off 7 messengers again, starting with Jon (the next person in the list, since Brad was removed). The seven are Jon, Zach, Alyce, Autumn, Jon, Zach, and Alyce. Autumn is declared safe and removed. Autumn was removed, so we now begin with Jon and count off as before. This time Zach is removed.

We begin the counting with Alyce, and Jon is removed. Since Alyce is the last one left, she will be the messenger to take the bad news.

 

Implementing the Bad News Bearer Program


Analysis Questions


  1. Assume you have a LinkedList object called llist.  You could get a normal (non-circular) ListIterator object for it with the method call llist.listIterator().  Both the linear iterator returned by the listIterator method and the circular iterator created by constructing a CircularLLIterator object implement the ListIterator interface, so both support the same set of methods.  Look over the list of methods in CircularLLIterator and identify which methods (if any) must be implemented differently for a linear or circular list iterator and which methods (if any) could share the same implementation if we created a new, abstract LLIterator class.
     
  2. You used an iterator returned by the iterator method for your initial list traversal of the messenger list, when you printed the names of all the messengers.  Could you have used a linear list iterator (an object returned by the listIterator method) for that traversal?  Could you have used a circular list iterator?  Why or why not?


* This problem was inspired by a classical mathematics problem known as the Josephus Problem. See, for example, Concrete Mathematics: A Foundation for Computer Science by Graham, Knuth, and Patashnik (Addison-Wesley, 1989). You could also refer to Section 8.3 in Java Software Structures: Designing and Using Data Structures, by Lewis and Chase (Addison-Wesley, 2005).


Authors: Autumn C. Spaulding autumn@max.cs.kzoo.edu
and Alyce Brady abrady@kzoo.edu