Homework 1 Solution

Homework 1 Solution - Spring 2009 CS 32 Homework 1 Solution...

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

View Full Document Right Arrow Icon
Spring 2009 CS 32 Homework 1 Solution Problems 1, 2, and 3 P Problem 4 P 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
Background image of page 1

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

View Full DocumentRight Arrow Icon
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); // If 0 <= i < uniqueSize(), copy into value an item in the multiset // and return the number of instances of that item in the multiset.
Background image of page 2
// 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. int find(const ItemType& value) const;
Background image of page 3

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

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

Page1 / 16

Homework 1 Solution - Spring 2009 CS 32 Homework 1 Solution...

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

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