CS 32 Solution Homework 4

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

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

Winter 2009 CS 32 Homework 4 Solution Problem 1 Problem 4 Problem 2 Problem 5 Problem 3 Problem 6 Problem 1: // Set.h #ifndef SET_INCLUDED #define SET_INCLUDED template <typename ItemType> class Set { public: Set(); // Create an empty set. bool empty() const; // Return true if the set is empty, otherwise false. int size() const; // Return the number of items in the set. bool insert(const ItemType& value); // Insert value into the set if it is not already present. Return // true if the value was actually inserted. Return false if the // value was not inserted (perhaps because it is already in the set // or because the set has a fixed capacity and is full). bool erase(const ItemType& value); // Remove the value from the set if present. Return true if the // value was removed; otherwise, leave the set unchanged and // return false. bool contains(const ItemType& value) const;

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

View Full Document
// Return true if the value is in the set, otherwise false. bool get(int i, ItemType& value) const; // If 0 <= i < size(), copy into value an item in the set and // return true. Otherwise, leave value unchanged and return false. void swap(Set <ItemType> & other); // Exchange the contents of this set with the other one. // Housekeeping functions ~Set(); Set(const Set <ItemType> & other); Set <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 struct Node { ItemType m_value; Node* m_next; Node* m_prev; }; Node* m_head; int m_size; void createEmpty(); // Create an empty list. (Will be called only by constructors.) void insertAtTail(const ItemType& value); // Insert value in a new Node at the tail of the
list, incrementing // m_size. void doErase(Node* p); // Remove the Node p, decrementing m_size. Node* find(const ItemType& value) const; // Return pointer to Node whose m_value == value if present, else m_head }; // Declarations of non-member functions template <typename ItemType> void unite(const Set <ItemType> <ItemType> & set2, Set <ItemType> & result); // result = { x | (x in set1) OR (x in set2) } template <typename ItemType> void subtract(const Set <ItemType> & set1, const Set <ItemType> <ItemType> & result); // result = { x | (x in set1) AND NOT (x in set2) } // Inline implementations template <typename ItemType> inline int Set <ItemType> ::size() const { return m_size; } template <typename ItemType> inline bool Set <ItemType> ::empty() const { return size() == 0; } template <typename ItemType> inline bool Set <ItemType> {

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

View Full Document
return find(value) != m_head; } // Non-inline implementations template <typename ItemType> Set <ItemType> ::Set() { createEmpty(); } template <typename ItemType> bool Set <ItemType> { // Fail if value already present
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

### Page1 / 18

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

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

View Full Document
Ask a homework question - tutors are online