Programming Assignment
Due:
Please make sure you adhere to the policies on
academic honesty.
Please see the general programming webpage for details about
the programming environment for this course, and specifically for
directions in how to submit your programming assignment electronically.
The files you need for this assignment can be
downloaded here.
Contents:
You will be writing a method which takes two individually sorted lists
(represented by singly-linked lists, as in Ch. 4.3), and merges
them together into one sorted list of the combined elements.
The general concept for merging two lists is quite simple when both
original lists are guaranteed to be sorted. In fact, such a merging
procedure turns out to be useful in designing an algorithm for sorting
a group of elements from scratch (which we will see later in the
course).
Before writing your code, think about how you might accomplish this by
hand. You could imagine the process by placing one finger at the
beginning of the first list and one finger at the beginning of the
second list. Now, you should be able to advance one of the two
fingers at each step, in such a way as to sift through the two lists,
creating one combined list which is perfectly sorted. Try this on an
example!
When it comes to programming the process, it would probably be easier
if we allowed you to create a brand new list for the output. However,
we will be intentionally making things a bit tougher as we ask you to
create the output by rearranging the next pointers of the
existing nodes from the original two lists, so as to remove items from
the second list and to sew each into the proper place in the first
list. In this way, the first list eventually contains all original
items, and the second list is effectively destroyed. All you need to
do is to turn this idea into working code.
The files you need for this assignment can be
downloaded here.
- IntNode.java is almost the same as the Node class
definition given in Code Fragment 4.10 of the text, except that we have
specialized it to deal specifically with elements of type int
(rather than using Object's) and we have intentionally
disabled one of the methods.
- MergeTest.java is our driver which will create the initial
linked lists based on information from the user (see below for the
input details). Then our main routine will call your method to merge
the two lists, and then the driver will output the results.
- Merge.java is a file which you will be writing. We have given
you a very basic template which you will modify.
You should submit three files:
- Merge.java - which contains all of the code you write
- readme - a brief summary of your program, and any
further comments you wish to make to the grader.
-
inputfile - you should provide a sample input file
which we will use in testing all of the other students' submissions. The
file format for inputfiles is based on use with the driver we provide below.
Your input file may use at most 100 lines (although you may
certainly test a variety of different merges in those 100 lines).
In order to make sure that your inputfile follows the proper format, we
strongly recommend that you run your program on your inputfile to make
sure it is accepted by the driver.
All of the code which you write should be in the file
Merge.java. What is required is that you define a static method
named mergelists which will accomplish the task with the
specifications outlined in the rest of this handout. You are free to
add any private routines which you find helpful in organizing your
task (though they too will have to be declared as static
routines). To aid in getting the precise syntax correct, we have
provided you with a template file Merge.java which appears as:
public class Merge {
public static IntNode mergelists(IntNode headerA,
IntNode headerB) {
}
}
The driver will take care of creating the initial singly-linked lists A and B
as described in Ch. 4.3.
Your routine should return the head of the resulting list. This list
should contain, in sorted (increasing) order, all of the elements
that were originally in the two lists. This means that you are
not creating a third new list with all of the elements,
rather you are simply rearranging the existing nodes into the desired
configuration. To further enforce this, we are making the following
demands:
- The expression "new IntNode()" should never appear in
your program. We want to force you to (carefully) rearrange
the next pointers of the existing nodes
to merge the two lists. We do not want you to create a brand new
list with brand new nodes, and then throw away the original lists.
- You are never to use the "IntNode.setElement()" method.
In rearranging the nodes of the list, there should never be a reason
for you to change the integer key which we are using for sorting (in
fact, we have intentionally disabled this method!)
Please note, although we have not allowed you to create new IntNode
objects, you may certainly (and will need to) have many variables
which reference an IntNode. That is, it is completely
acceptable to declare a variable such as "IntNode temp;"
in your program.
The file MergeTest.java is a driver which will handle reading
the input, creating the initial lists, calling your routine, and then
outputting the results. In fact, as a wonderful aide, our driver even
calls a "validation" method at the end, in order to test the
correctness of your method on that particular input.
The expected format for input is as follows
(please make sure that the inputfile you submit adheres
strictly to these standards!):
- Each line is to contain a single integer.
- The end of a particular list is indicated by 1000.
- The final end of data is indicated by -1.
- All other integer values must be between 0 and 999.
- Each individual list must have elements entered in increasing order.
- Duplicate values may appear in one or both of the lists. In this
case, the final merged list will contain multiple copies of those
particular values.
- The file is at most 100 lines.
You will notice that this format allows you to specify a test file
which involves many different merges, as demonstrated in the following
example of a properly formatted input file.
5
9
12
1000
4
10
11
15
1000
3
4
1000
1000
1
5
5
8
1000
2
8
10
1000
-1
The driver will create the two lists {5, 9, 12} and
{4, 10, 11, 15}, and then call the mergelist
routine (which hopefully produces
{4, 5, 9, 10, 11, 12, 15}).
After that, it will create two lists
{3, 4} and { }, and then call mergelist (which
hopefully produces the list {3, 4}).
Then it will call mergelist with
{1, 5, 5, 8} and {2, 8, 10},
hopefully producing the list
{1, 2, 5, 5, 8, 8, 10}. After
this, it exits.
By default, the driver reads input from the keyboard.
However, if you would like to have the driver read input from a text
file, you may create such a file, and then give the filename as a
single argument when starting the program (as was done in an
earlier programming assignment).
The assignment is worth 10 points. Eight points will be
awarded based on our own evaluation of your assignment and the readme
file. One additional point will be awarded fractionally based on how
well your program performs on other students' test inputs. The final
point will be awarded fractionally based on how well your test input
fools other students' flawed programs.
Last modified: 11 September 2002