Homework 4 Solution

# Homework 4 Solution - Spring 2009 CS 32 Homework 4 Solution...

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

Spring 2009 CS 32 Homework 4 Solution Problem 1 Problem 4 Problem 2 Problem 5 Problem 3 Problem 6 Problem 1: // Multiset.h #ifndef MULTISET_INCLUDED #define MULTISET_INCLUDED #include <algorithm> // for std::swap template #include <cassert> template <typename ItemType> 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 iff the vakue could be // inserted. (For a linked list representations,

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

View Full Document
insert always returns // true.) 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 *lt;= i *lt; 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 <ItemType> & other); // Exchange the contents of this multiset with the other one. // Housekeeping functions ~Multiset(); Multiset(const Multiset <ItemType> & other); Multiset <ItemType> <ItemType> & rhs); private: // Representation: // a circular doubly-linked list with a dummy node. // m_head points to the dummy node.
// m_head->m_prev->m_next == m_head and m_head- >m_next->m_prev == m_head // m_size == 0 iff m_head->m_next == m_head- >m_prev == m_head // If m_size > 0 // m_head->next points to the node at position 0. // m_head->prev points to the node at position m_size-1. // Nodes are in no particular order. struct Node { ItemType m_value; int m_count; Node* m_next; Node* m_prev; }; Node* m_head; int m_uniqueSize; int m_size; void createEmpty(); // Create an empty list. (Will be called only by constructors.) void insertAtTail(const ItemType& value, int count); // Insert value with its count in a new Node at the tail of the list, // incrementing m_uniqueSize and m_size. Node* find(const ItemType& value) const;

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.

{[ snackBarMessage ]}

### Page1 / 18

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

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

View Full Document
Ask a homework question - tutors are online