project2

project2 - Spring 2009 CS 32 Programming Assignment 2...

Info iconThis preview shows pages 1–2. Sign up to view the full content.

View Full Document Right Arrow Icon
Spring 2009 CS 32 Programming Assignment 2 Double Trouble 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; class Multiset { public: Multiset(); 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: Destructor When a Multiset is destroyed, the nodes in the linked list must be deallocated. Copy constructor 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. Assignment operator 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
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Image of page 2
This is the end of the preview. Sign up to access the rest of the document.

This note was uploaded on 01/24/2012 for the course CS CS 32 taught by Professor Mojavi during the Spring '08 term at UCLA.

Page1 / 4

project2 - Spring 2009 CS 32 Programming Assignment 2...

This preview shows document pages 1 - 2. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online