Spring 2009 CS 32
Programming Assignment 2
Time due: 9:00 PM Tuesday, April 21
Homework 1 gave you extensive experience with the Multiset ADT using both arrays and dynamically-allocated arrays.
In this project, you will re-write the implementation of your ADT to employ a doubly-linked list rather than an array.
You must not use arrays. You will also implement a couple of algorithms that operate on multisets.
Implement Multiset yet again
Consider the Multiset interface from problem 2 of Homework 1:
typedef TheTypeOfElementGoesHere ItemType;
bool empty() const;
int size() const;
int uniqueSize() const;
bool insert(const ItemType& value);
int erase(const ItemType& value);
int eraseAll(const ItemType& value);
bool contains(const ItemType& value) const;
int count(const ItemType& value) const;
int get(int pos, ItemType& value) const;
void swap(Multiset& other);
In problem 3 of Homework 1, you implemented this interface using an array. For this project, implement the Multiset
interface using a doubly-linked list. (You must not use the list class template from the C++ library.)
For the array implementation of problem 3 of Homework 1, since you declared no destructor, copy constructor, or
assignment operator, the compiler wrote them for you, and they did the right thing. For this linked list implementation,
if you let the compiler write the destructor, copy constructor, and assignment operator, they will do the wrong thing, so
you will have to declare and implement these public member functions as well:
When a Multiset is destroyed, the nodes in the linked list must be deallocated.
When a brand new Multiset is created as a copy of an existing Multiset, enough new nodes must be allocated to hold a
duplicate of the original list.
When an existing Multiset (the left-hand side) is assigned the value of another Multiset (the right-hand side), the result
must be that the left-hand side object is a duplicate of the right-hand side object, with no memory leak of list nodes (i.e.
no list node from the old value of the left-hand side should be still allocated yet inaccessible).
Notice that there is now no a priori limit on the maximum number of items in the Multiset.
Another requirement is that as in Problem 5 of Homework 1, the number of statement executions when swapping two
multisets must be the same no matter how many items are in the multisets.
Implement some multiset algorithms
Using only the public interface of Multiset, implement the following two functions. (Notice that they are non-member
functions; they are not members of Multiset or any other class.)
void combine(const Multiset& ms1, const Multiset& ms2, Multiset& result);
When this function returns, result must contain a copy of all the elements that appear in ms1 or ms2 or both, and must
not contain any other values. (You must not assume result is empty when it is passed in to this function.) If a value