You will be writing a method which takes a sorted, doubly linked lists, and finds the median element(s) of this list. Informally, the median of a sorted list is the "halfway point" of the list. For example, if the list is {1, 5, 6, 8, 20, 33, 50}, then 8 is the median element because it has three elements on each side of it. When the list has an even number of elements, there are technically two medians, for example the list {1, 6, 8, 20, 33, 50} has medians 8 and 20. In general, if you know the length of a sorted list, then you know exactly in which position the median(s) will be.
If the data structure for a list contains a size method, writing code to find the median would simply require walking the correct number of steps into the middle of the list. However, in this lab, we will be providing your routine with a list, but we will not be providing you the size of the list. Furthermore, in this lab we ask that you not calculate the size of the list by walking the entire list; the time spent doing this is unnecessary.
If you are careful, you should be able to find the median(s) by simultaneously walking from both ends of the list towards the center. When these two walks approach each other, you should be able to determine the median element(s). Be Careful: Determining when the walks are meeting each other will be slightly different for the case of an odd-lengthed list and an even-lengthed list. You should probably start by focusing on one case, and then once you have it working, think again about how you can detect the other case.
Although you will only need to modify one file (Median.java), you will need to
download several files we provide.
The file MedianTest.java is a driver which will handle reading
the input from the keyboard, creating the initial lists, and calling
your routine.
The driver contains a loop which reads in a previously sorted list of
integers. After building a list, it calls your routine. After your
routine returns, the driver asks the user to give a new list, and
continues in this way. The exact details for the input are:
For example, if the test data is:
The output may look something like:
All of the code which you write should be in the file
Median.java. Specifically, we have defined a static method named
printMedian which you are to write. This routine is defined as follows:
The driver will be creating a doubly linked list which contain a
header and a trailer sentinel ("dummy node") at each end of the list
as discussed in Ch. 4.4.
Your routine should not modify the list in any way. Rather you should
use the list to find the node(s) containing the median(s). Once you
have found the median(s), your program should create a line of output
displaying the median(s). Make sure your program correctly handles
the case of a single median (with an odd-lengthed list) or the case of
two medians (with an even-lengthed list). Sample output might be:
if the list has odd length. Alternatively, if the list has even length,
IntDLNode.java is exactly the same as the DLNode class
definition given in Section 4.4 of the text, except that we have
specialized it to deal specifically with elements of type int
as opposed to Object.
Specifically, you will want to use the inquiry methods:
You do not have to submit any files electronically. You should
instead make a printout of the modified file Median.java and
make sure your name is written clearly at the top of the file.
Step 1 - compiling and running the Driver
1
5
6
8
20
33
50
1000
1
6
8
20
33
50
1000
-1
Original list: 1 5 6 8 20 33 50
The median element is 8.
Original list: 1 6 8 20 33 50
The median elements are 8 and 20.
Step 2 - Your task
public class Median {
public static void printMedian(IntDLNode thehead, IntDLNode thetail) {
}
}
The median element is 8.
The median elements are 8 and 20.
IntDLNode class
int getElement();
IntDLNode getNext();
IntDLNode getPrev();
Handing in your completed lab
Last modified: 4 September 2002