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:

  • Overview
  • Files you will need
  • Files to Submit
  • Requirements and Specifications
  • Using our Tester (input/output)
  • Grading Standards

  • Overview

    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.

    Files you will Need

    The files you need for this assignment can be downloaded here.

    Files to Submit

    You should submit three files:

    Requirements and Specifications

    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:

    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.

    Using our Tester (input/output)

    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!):

    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).


    Grading Standards

    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