Assignments | Class Photo | Computing Resources | Course Home | Lab Hours/Tutoring | Schedule | Submit

Saint Louis University

Computer Science 180
Data Structures

Michael Goldwasser

Fall 2008

Dept. of Math & Computer Science

Programming Assignment 04

Merging Lists

Due: Thursday, 30 October 2008, 11:59pm

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 manipulate list of integers and iterators for those lists. Given two lists, both of which are known to be sorted in non-decreasing order, your goal is to efficiently merge the elements from the second list into the first list, while ensuring that the combined result is still sorted. Because the two original lists are internally sorted, the merging process can be completed in linear time.

The source code for the entire project is already complete, with the exception of a single routine that we ask you to implement.

/**
 * Given two distinct lists, each of which are assumed to be sorted in
 * non-decreasing order, this function mutates the primary list so as
 * to include all elements of the secondary list while maintaining the
 * non-decreasing invariant.
 */
void merge(list& primary, const list& secondary)
Notice that we have designated the secondary list as immutable with the const designation. You are not to remove any values from that list, but to reinsert those values in the proper place in the primary list.

Before writing your code, think about how you will accomplish the task. It may help to consider the process you might use if doing this with lists written on paper. 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 fingers in a way, ensuring that items are copied from the second list into the proper place in the first list as you go. To implement this strategy, the iterator classes can be used to represent the idea of a finger moving through a list.

Advice: if you are having trouble, always consider that a problem can be fixed by simplifying your code rather than complicating it. There exists a remarkably simple solution to this assignment.


Files We Are Providing

All such files can be downloaded here.


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 merged result.

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 invoke merge(A,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 merge(A,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).


Files to Submit


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
CSCI 180, Fall 2008
Last modified: Wednesday, 22 October 2008
Assignments | Class Photo | Computing Resources | Course Home | Lab Hours/Tutoring | Schedule | Submit