Question: Using the list class(list.h), implement a Set class(set.cpp) that does the following: Implements the following functions: set_union : Creates and returns a Set pointer that
Using the list class(list.h), implement a Set class(set.cpp) that does the following: Implements the following functions: set_union : Creates and returns a Set pointer that contains the set union of the invoking Set object and a second Set object passed into the function. Note: union is a reserved keyword in C++, which is why the identifier for this function is difference from the other two set functions. intersection : Creates and returns a Set pointer that contains the set intersection of the invoking Set object and a second Set object passed into the function. difference : Creates and returns a Set pointer that contains the set difference between the invoking Set object and a second Set object passed into the function. The invoking Set object should be considered the set that is being subtracted from ( Invoking Set Parameter Set). print : Print the contents of the set. Use the following output format: "set contents(
list.h:
#includeclass ListNode { private: int data; ListNode* prev; ListNode* next; public: ListNode() { prev = next = NULL; } ListNode(int d, ListNode* p, ListNode* n) { data = d; prev = p; next = n; } friend class List; }; class List { private: ListNode* head; ListNode* tail; public: List() { head = tail = NULL; } ~List(); bool isEmpty() { return head == NULL; } bool contains(int value); void addToHead(int value); void addToTail(int value); int removeHead(); int removeTail(); int removeAt(int index); bool remove(int value); int at(int index); int valueOf(const ListNode* elem); const ListNode* getNext(const ListNode* node); const ListNode* getPrevious(const ListNode* node); const ListNode* getHead() { return head; } const ListNode* getTail() { return tail; } };
List::~List() { while (!isEmpty()) removeTail(); } bool List::contains(int value) { ListNode *temp = head; while (temp != NULL && temp->data != value) temp = temp->next; return temp != NULL; } void List::addToHead(int value) { if (isEmpty()) { head = tail = new ListNode(value, NULL, NULL); } else { head = new ListNode(value, NULL, head); head->next->prev = head; } } void List::addToTail(int value) { if (isEmpty()) { head = tail = new ListNode(value, NULL, NULL); } else { tail = new ListNode(value, tail, NULL); tail->prev->next = tail; } } int List::removeHead() { int value = head->data; if (head == tail) { delete tail; head = tail = NULL; } else { head = head->next; delete head->prev; head->prev = NULL; } return value; } int List::removeTail() { int value = head->data; if (head == tail) { delete tail; head = tail = NULL; } else { tail = tail->prev; delete tail->next; tail->next = NULL; } return value; } int List::removeAt(int index){ if (index < 0 || head == NULL) exit(0); int count = 0; ListNode *curr = head; if (index == 0){ int a = head->data; head = head->next; delete curr; return a; } ListNode *q = curr; while (curr != NULL && count != index){ q = curr; curr = curr->next; count++; } if (count == index){ int a = curr->data; q->next = curr->next; delete curr; return a; } else { exit(0); } } bool List::remove(int value){ ListNode *p = head; if (p == NULL){ return false; } if (p->data == value){ head = head->next; delete p; return true; } ListNode *q; q = p; while (p!= NULL && p->data != value){ q = p; p = p->next; } if (p == NULL){ return false; } else { q->next = p->next; delete p; return true; } } int List::at(int index){ ListNode *curr = head; if (index < 0 || head == NULL) exit(0); int count = 0; ListNode *p = head; if (index == 0){ return head->data; } while (curr != NULL && count != index){ curr = curr->next; count++; } if (count == index){ return curr->data; } else { exit(0); } } int List::valueOf(const ListNode *node){ return node->data; } const ListNode* List::getNext(const ListNode* node){ return node->next; } const ListNode* List::getPrevious(const ListNode* node){ return node->prev; }
set.cpp:
#include#include "list.h" using namespace std; class Set { private: List* list; int set_size; public: Set(){ list = new List(); set_size = 0; } ~Set(){ clear(); } bool contains(int value) { return list->contains(value); } bool add(int value){ if(list->contains(value)){ return false; } else{ list->addToHead(value); ++set_size; return true; } } bool remove(int value){ if(!list->contains(value)){ return false; } else{ list->remove(value); --set_size; return true; } } void clear() { set_size = 0; delete list; } Set* set_union(Set&){}; Set* intersection(Set&){}; Set* difference(Set&){}; void print() int size() { return set_size; } };
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
