hw4 slon

hw4 slon - Spring 2009 CS 32 Homework 4 Solution Problem 1...

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

View Full Document Right Arrow Icon
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, 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.
Background image of page 1

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

View Full DocumentRight Arrow Icon
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.
Background image of page 2
Image of page 3
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 / 13

hw4 slon - Spring 2009 CS 32 Homework 4 Solution Problem 1...

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

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