cs32homeworksolutions

# cs32homeworksolutions - Homework 1 Solution Problems 1 2...

This preview shows pages 1–3. Sign up to view the full content.

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

View Full Document
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: Homework 1 Solution Problems 1, 2, and 3 Problem 4 Problem 5 Problems 1, 2, and 3: In this solution, the functions with small, fast implementations are inlined. Alternatively, the inline keyword can be removed and the function definitions moved to Multiset.cpp. ( inline will be mentioned at some point in class, so don't worry if you've never seen it before.) Notice which member functions are const, and observe the use of the typedef name ItemType . // Multiset.h #ifndef MULTISET_INCLUDED #define MULTISET_INCLUDED // Later in the course, we'll see that templates provide a much nicer // way of enabling us to have Multisets of different types. For now, we'll // use a typedef. typedef unsigned long ItemType; const int DEFAULT_MAX_ITEMS = 200; class Multiset { public: Multiset(); // Create an empty multiset. bool empty() const ; // Return true if the multiset is empty, otherwise false. int size() const ; // Return the number of items in the multiset. For example, the size // of a multiset containing "cumin", "cumin", "cumin", "turmeric" is 4. int uniqueSize() const ; // Return the number of distinct items in the multiset. For example, // the uniqueSize of a multiset containing "cumin", "cumin", "cumin", // "turmeric" is 2. bool insert(const ItemType & value); // Insert value into the multiset. Return true if the value was // actually inserted. Return false if the value was not inserted // (perhaps because the multiset has a fixed capacity and is full). int erase(const ItemType & value); // Remove one instance of value from the multiset if present. // Return the number of instances removed, which will be 1 or 0. int eraseAll(const ItemType & value); // Remove all instances of value from the multiset if present. // Return the number of instances removed. bool contains(const ItemType & value) const ; // Return true if the value is in the multiset, otherwise false. int count(const ItemType & value) const ; // Return the number of instances of value in the multiset. int get(int i, ItemType & value) const ;; // If 0 <= i < uniqueSize(), copy into value an item in the multiset // and return the number of instances of that item in the multiset. // Otherwise, leave value unchanged and return 0. void swap(Multiset& other); // Exchange the contents of this multiset with the other one. private: // Since this structure is used only by the implementation of the // Multiset class, we'll make it private to Multiset. struct Node { ItemType m_value; int m_count; }; Node m_data[DEFAULT_MAX_ITEMS]; // the items in the multiset, with counts int m_uniqueSize; // how many distinct items in the multiset int m_size; // total number of items in the multiset // At any time, the elements of m_data indexed from 0 to m_uniqueSize-1 // are in use. m_size is the sum of the m_counts of those elements....
View Full Document

## This note was uploaded on 03/11/2010 for the course CS 31 taught by Professor Melkanoff during the Fall '00 term at UCLA.

### Page1 / 51

cs32homeworksolutions - Homework 1 Solution Problems 1 2...

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

View Full Document
Ask a homework question - tutors are online