CS 367 - Introduction to Data Structures - Section 3

Programming Assignment One: Sets

Due Tuesday, October 28, 1997 by 2:30pm

(This assignment is at least an order of magnitude more complex than the previous assignment. Please start early!)

Contents


Motivation

Sets are container classes like bags and lists. Like bags, but unlike lists, sets are unordered. Unlike for bags and lists, duplication of elements is not allowed for sets.

As an example, imagine you are a disc jockey for a local college radio station (I said imagine). Part of your job is to take requests for songs. The radio station has a policy of playing no repeats (at least on the same day), but sometimes the requests are redundant. If we store the requests in a set, then repetition can be avoided, and we won't necessarily play the songs in the request order (which is good since it can prevent musical clashes, such as hearing Metallica right after Prince).

Sets are extremely useful in implementing algorithms related to other fundamental data structures, as we will see later in the semester when we look at graphs. To get a flavor for the kind of roles sets will play, suppose that you want to know how many cities you can fly to via commercial airlines from Madison making no more than one stop en route. We might consider first the collection of all cities that can be reached from Madison nonstop. We could add these to a list. Then we might consider all the cities that can be reached from those cities and then add them to our list. But there will likely be lots of duplication on the list. (For example, we can fly from Madison to Chicago to New York, from Madison to Detroit to New York, and from Madison to Minneapolis to New York.) If we use sets, we avoid this problem.

We might want to then consider each of these cities in turn as candidates for a trip. But we don't necessarily want to consider them in any particular order. Lists impose an order even when we don't want one. Sets do not.

Of course, we could avoid the second problem by just ignoring the order of the list and we could avoid the first problem by checking before adding an element to see if it was already on our list. This makes a nice segue into what this assignment is about: implementing a set class using a list class.

(For more info about sets, read below.)


The Assignment

This assignment concerns the implementation of a template class Set using a template class List.

The assignment is intended to:


Requirements

In the true spirt of container classes, we don't want our sets (or our lists) to be constrained to be of just one type, so we use template classes.

You have three (rather extensive) requirements for this assignment:

  1. For the List template class, write the isLast, isMember, and find member functions as specified in the documentation atop the file list.h . You will have to add prototypes in list.h as well as implementation in list.C.
  2. For the List template class, write a destructor, copy constructor, and assignment operator according to the specification atop list.h . Again, you will have to add prototypes in list.h as well implementation in list.C.
  3. Implement the entire Set template class according to the documentation provided atop set.h. You should write the declaration for the template class below the documentation in set.h (where indicated by comments); you should put the member function definition in a separate file set.C.

Grading

The program will be graded on an 100 point scale, weighted as follows: I cannot stress enough the importance of documentation - poorly documented programs may be graded harshly. For more information on grading policy, click here.


Files

There are five files you will need to do the assignment, found in ~cs367-3/Assignments/One/Files: You can copy the files by viewing the documents in Netscape and then using the Save As option from the File menu. Alternatively, you can simply copy them to the directory of your choice. For example to copy them to the directory ~foo/cs367 you can type:
cp ~cs367-3/Assignments/One/Files/* ~foo/cs367

You should expect to modify list.h , list.C list.h , and set.h . You should plan on crating a file set.C for the implementation of the Set template class member functions. You may modify main.C and Makefile, but be advised that the compilation grade is dependent upon your other files working properly with the given main.C and Makefile. (You may want to make a different main and Makefile for testing out the list application - you could base such a program on the test program used for the bag assignment, or on a test program used to for the list classes discussed in class, such as the one here.)


How to get started


Sets

Sets are notated as elements, comma separated between curly braces, as in: {3,1,2}, {"hello","goodbye"}, and {{3.14,2.71},{}}. We consider Sets to be homogeneous - for a given set, all its elements are of the same type.

Basic operations on sets include:

The empty set {} is the set which contains nothing. {x} is a "singleton" set containg one element, x. If E is an empty set then {x} might be formed by the command E.add(x).

{x,y} is a set containing x and y. Order does not matter in sets, so {x,y} is the same as {y,x}.

Two sets are equal if and only if all the elements in the first set are present in second set and all the elements in the second set are present in the first.

If S and T are equiavlent sets, then after the operations:

S.add(x); 
S.add(y);
T.add(y);
T.add(x);
S and T are still equal.

Duplication does not occur in sets. If S and T are equal, then after the operations:

S.add(x);
T.add(x);
T.add(x);
T.add(x);
T.add(x);
S and T are still equal.

Binary operations are often defined on sets, such as:


Additions to List

In class we talked about classes for lists of one specific type (either int or String). The List template class is a generalization of those classes, with a few changes. In particular, three additional private members have been added: Look at the code in list.h and list.C for more details.


Related Reading

C++ topics relevant to this assignment covered in the texts: Documentation for the debugger, gdb, can be found here.


Submitting

What we need once you are done: your versions of list.h, list.C, set.h, and set.C. You can submit additional files if necessary, but bear in mind that the compile grade hinges upon our ability to compile your file with the original main.C and Makefile. Copy the necessary files to the directory ~cs367-3/Submissions/One/<login> where <login> is your login name. For example if your login name is foo, then from the directory where your modified bag.C file is located, type:
cp bag.C ~cs367-3/Submissions/One/foo 
This is a paperless assignment. You need only turn in the program electronically.


Solution

The instructor's solution is now available here. A compiled executable derived from the solution is available for you to try out. The solution executable can be found at ~cs367-3/Solutions/One/sets

Late Policy

Late assignments will not be accepted. If you feel you truly have a justifiable excuse for turning in the assignment late, you must clear it with me at least one day in advance. (I do not plan on clearing any such requests.)