Course Home | Homework | Programming | Schedule & Lecture Notes | Submit

Saint Louis University

Computer Science A220/P126
Data Structures and Object-Oriented Programming

Michael Goldwasser

Spring 2005

Dept. of Math & Computer Science

Programming Assignment 05

Merge

Due: Thursday, 17 March 2005, 8pm

Please see the general programming webpage for details about the programming environment for this course, guidelines for programming style, and details on electronic submission of assignments.

Collaboration Policy

For this assignment, you must work individually in regard to the design and implementation of your project.

Please make sure you adhere to the policies on academic integrity in this regard.


Contents:


Overview

Our goal for this assignment is to maintain lists of integers which are sorted in non-decreasing order. In particular, we focus on an operation for merging two such lists while ensuring that the data is sorted in the combined result. Conceptually, the merging process is simple precisely because the two original lists are internally sorted.

You will be writing a method merging two such lists, represented as doubly-linked lists with header and trailer sentinels, as in Ch. 4.5. Specifically, we provide a class SortedList to create and represent such lists. The source code for the entire project is already complete, with the exception of a single routine which we ask you to implement. The class definition for the SortedList class includes the following method declaration.

  /**
   * Removes all elements from list 'that' merging the nodes into the
   * current list 'this', while maintaiing the invariant that the
   * elements are sorted.
   */
  void merge(SortedList* that);
You must implement this method. The method should removing all elements from the second list, parameter that, merging those nodes into the current list (i.e. this), while maintaining the invariant that the elements of the resulting list are in non-decreasing order. Moreso, we have structured the class definitions so that you cannot create new nodes! Your only way to accomplish the task will be to rearrange the prev and next pointers of the existing nodes of the two lists.

So all you have to do is to turn this idea into working code. Before writing your code, think about how you will accomplish this. It may help to consider the process you might use if physically manipulating objects, such as two groups of books. You might imagine 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 the pointers in a way, ensuring that items are moved from the second list into the proper place in the first list as you go. If you are having trouble as you go, always think about if a bug could be fixed by simplifying your code before you decide to expand your code. There exists a remarkably simple solution to this assignment.

In fact, the merging process is described on page 491 of the text, due to its role in the larger context of designing an algorithm for sorting a group of elements from scratch. You may skim that material in the book for general guidelines regarding the merging process, however the implementation details for this assignment vary greatly from that coverage in the book. In particular, the description in the book creates a third list as the result, rather than directly reogranizing the items in the original lists. Furthermore, the algorithm in the book is described using the Sequence ADT rather than by directly manipulating a low-level linked lists as in this assignment.


Files We Are Providing

All such files can be downloaded here.


Files to Submit


Using Our Driver

The executable MergeTest is a driver which will handle reading the input, creating the initial lists, calling your routine, and then outputting the results. The merged list is then printed twice, once while traversing the list from front to back, once while traversing from back to front (in case inconsistencies exist in the doubly-linked list).

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 A = {5, 9, 12} and B = {4, 10, 11, 15}, and then call invoke method A.merge(B). Hopefully this results in A = {4, 5, 9, 10, 11, 12, 15}.

After that, it will create two lists A = {3, 4} and B = { }, and again call A.merge(B) (which hopefully results in A = {3, 4}).

Then it will merge {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.


Michael Goldwasser
CS A220/P126, Spring 2005
Last modified: Wednesday, 06 April 2005
Course Home | Homework | Programming | Schedule & Lecture Notes | Submit