Question

In this project you will design, implement and test the ADT set using both Arrays and Linked Lists and implement all the oper

{x|X E A 1 x E B} Example: The intersection of {1,2,3} and {3,4,5} is: {3}. Definition: Let A and B be sets. The difference oPLEASE ANSWER THE FOLLOWING IN C++!! PLEASE READ THE QUESTION CAREFULLY!!! AS WELL AS WHOEVER ANSWERS THIS CORRECTLY I WILL UPVOTE!!!

0 0
Add a comment Improve this question Transcribed image text
Answer #1

LinkedList.h

======================

#pragma once

template <typename T>
class LinkedList {
private:
   // private node class to store internal data
   class Node {
   public:
       T data;
       Node* next;
       Node(T item) {
           data = item;
           next = nullptr;
       }
   };
   Node* head; // head of list
   int count; // number of elements in the list
public:
   LinkedList(); // constructor
   ~LinkedList(); // destructor
   int size(); // returns size of list
   void add(T item); // add given item at start of list
   void remove(T item); // removes given item from the list if it present
   T get(int index); // returns item at given index from list
   bool contains(T item); // return true if given item is in list
};

template <typename T>
LinkedList<T>::LinkedList() {
   // initialize variables
   head = nullptr;
   count = 0;
}

template <typename T>
LinkedList<T>::~LinkedList() {
   while (head != nullptr) {
       Node* temp = head;
       head = head->next;
       delete temp;
   }
}

template <typename T>
void LinkedList<T>::add(T item) {
   // create new node
   Node* node = new Node(item);
   // assign new node to head
   node->next = head;
   // re-assign head
   head = node;
   count++;
}

template <typename T>
void LinkedList<T>::remove(T item) {
   // create iterator
   Node* itr = head;
   // check if head is need to be removed
   if (head->data == item) {
       head = head->next;
       delete itr;
       count--;
       return;
   }
   // loop the list to find item
   for (int i = 1; i < size(); i++) {
       if (itr->next->data == item) {
           // remove next node
           // check if next node is last node
           if (itr->next->next == nullptr) {
               Node* temp = itr->next;
               itr->next = nullptr;
               delete temp;
               count--;
               return;
           }
           else {
               Node* temp = itr->next;
               itr->next = itr->next->next;
               delete temp;
               count--;
               return;
           }
       }
       itr = itr->next;
   }
}

template <typename T>
int LinkedList<T>::size() {
   return count;
}

template <typename T>
T LinkedList<T>::get(int index) {
   // create iterator
   Node* itr = head;
   for (int i = 0; i < index; i++) {
       itr = itr->next;
   }
   return itr->data;
}

template <typename T>
bool LinkedList<T>::contains(T item) {
   // create iterator
   Node* itr = head;
   // loop the list
   for (int i = 0; i < count; i++) {
       if (itr->data == item) {
           return true;
       }
       itr = itr->next;
   }
   return false;
}

==============================

MathSet.h

==============================

#pragma once
#include "LinkedList.h"

template <typename T>
class MathSet {
private:
   LinkedList<T>* list; // linked list to store elements in set
public:
   MathSet(); // constructor
   ~MathSet(); // destructor
   void add(T element); // adds given element to set if it doesn't exist in set
   void remove(T element); // removes given element from set
   bool contains(T element) const; // returns true if given element is in the set
   bool is_equal(const MathSet<T>& set); // returns true if this set is equal to given set
   bool is_subset(const MathSet<T>& set); // returns true if this set is subset of given set
   int cardinality() const; // return total number of elements in set
   MathSet<T>& make_union(const MathSet<T>& set); // returns a new set that is union of current set and given set
   MathSet<T>& intersection(const MathSet<T>& set); // returns a new set that is intersection of current set and given set
   MathSet<T>& difference(const MathSet<T>& set); // returns a new set that contains elements of this set that are not present in given set
   T get(int index); // returns element t given index
};


template <typename T>
MathSet<T>::MathSet() {
   // initialize variables
   list = new LinkedList<T>();
}

template <typename T>
MathSet<T>::~MathSet() {
   delete list;
}

template <typename T>
void MathSet<T>::add(T element) {
   // check if given element is not in list
   if (!list->contains(element)) {
       list->add(element);
   }
}

template <typename T>
void MathSet<T>::remove(T element) {
   // remove element from list
   list->remove(element);
}

template <typename T>
bool MathSet<T>::contains(T element) const{
   return list->contains(element);
}

template <typename T>
bool MathSet<T>::is_equal(const MathSet<T>& set) {
   // check size of sets
   if (cardinality() != set.cardinality()) {
       return false;
   }
   // loop current set
   for (int i = 0; i < cardinality(); i++) {
       // check if each elemnt is present in given set
       if (!set.contains(this->list->get(i))) {
           return false;
       }
   }
   return true;
}

template <typename T>
bool MathSet<T>::is_subset(const MathSet<T>& set) {
   // check size of sets
   if (this->cardinality() > set.cardinality()) {
       return false;
   }
   // loop current set
   for (int i = 0; i < cardinality(); i++) {
       // check if each elemnt is present in given set
       if (!set.contains(this->list->get(i))) {
           return false;
       }
   }
   return true;
}

template <typename T>
int MathSet<T>::cardinality() const{
   return list->size();
}

template <typename T>
MathSet<T>& MathSet<T>::make_union(const MathSet<T>& set) {
   // create a new set to return
   MathSet<T>* result = new MathSet<T>();
   // first add elements from current set
   for (int i = 0; i < this->cardinality(); i++) {
       result->add(this->list->get(i));
   }
   // add element from second set
   for (int i = 0; i < set.cardinality(); i++) {
       result->add(set.list->get(i));
   }
   // return the resulting set
   return *result;
}

template <typename T>
MathSet<T>& MathSet<T>::intersection(const MathSet<T>& set) {
   // create a new set to return
   MathSet<T>* result = new MathSet<T>();
   // loop the current set
   for (int i = 0; i < this->cardinality(); i++) {
       // check if elemet from current set present on both set
       if (set.contains(this->list->get(i))) {
           // add element to the result set
           result->add(this->list->get(i));
       }
   }
   // return the resulting set
   return *result;
}

template <typename T>
MathSet<T>& MathSet<T>::difference(const MathSet<T>& set) {
   // create a new set to return
   MathSet<T>* result = new MathSet<T>();
   // first add all elements from current set
   for (int i = 0; i < this->cardinality(); i++) {
       result->add(this->list->get(i));
   }
   // remove all element from second set
   for (int i = 0; i < set.cardinality(); i++) {
       result->remove(set.list->get(i));
   }
   // return the resulting set
   return *result;
}

template <typename T>
T MathSet<T>::get(int index) {
   return list->get(index);
}

=================================

main.cpp

=================================

#include <iostream>
#include "mathSet.h"
using namespace std;

int main() {
   // test all function from MathSet
   cout << "for testing create sets of characters" << endl;
   cout << "create empty set" << endl;
   MathSet<char> set1;
   cout << "size of set : " << set1.cardinality() << " expected : 0" << endl;
   cout << "adding elements to list" << endl;
   set1.add('A');
   set1.add('B');
   set1.add('A');
   cout << "size of set : " << set1.cardinality() << " expected : 2" << endl;
   cout << "creating second set" << endl;
   MathSet<char> set2;
   set2.add('C');
   set2.add('B');
   set2.add('D');
   cout << "set contains : ";
   for (int i = 0; i < set2.cardinality(); i++) {
       cout << set2.get(i) << " ";
   }
   cout << "expected : D B C" << endl;

   MathSet<char> set3;
   set3.add('B');
   set3.add('A');

   cout << "testing equality on set {B A} and {A B} :";
   if (set1.is_equal(set3)) {
       cout << " true ";
   }
   else {
       cout << " false ";
   }
   cout << "expected : true" << endl;
   cout << "testing equality on set {B A} and {D B C} :";
   if (set1.is_equal(set2)) {
       cout << " true ";
   }
   else {
       cout << " false ";
   }
   cout << "expected : false" << endl;

   cout << "testing union on set {B A} and {D B C} : ";
   MathSet<char> set4 = set1.make_union(set2);
   for (int i = 0; i < set4.cardinality(); i++) {
       cout << set4.get(i) << " ";
   }
   cout << "expected : B A D C" << endl;

   cout << "testing subset on set ";
   for (int i = 0; i < set2.cardinality(); i++) {
       cout << set2.get(i) << " ";
   }
   cout << "and set ";
   for (int i = 0; i < set4.cardinality(); i++) {
       cout << set4.get(i) << " ";
   }
   cout << ": ";
   if (set2.is_subset(set4)) {
       cout << " true ";
   }
   else {
       cout << " false ";
   }
   cout << "expected : true" << endl;

   cout << "testing subset on set ";
   for (int i = 0; i < set4.cardinality(); i++) {
       cout << set4.get(i) << " ";
   }
   cout << "and set ";
   for (int i = 0; i < set2.cardinality(); i++) {
       cout << set2.get(i) << " ";
   }
   cout << ": ";
   if (set4.is_subset(set2)) {
       cout << " true ";
   }
   else {
       cout << " false ";
   }
   cout << "expected : false" << endl;

   MathSet<char> set5 = set1.intersection(set4);
   cout << "testing intersection on set ";
   for (int i = 0; i < set1.cardinality(); i++) {
       cout << set1.get(i) << " ";
   }
   cout << "and set ";
   for (int i = 0; i < set4.cardinality(); i++) {
       cout << set4.get(i) << " ";
   }
   cout << ": ";
   for (int i = 0; i < set5.cardinality(); i++) {
       cout << set5.get(i) << " ";
   }
   cout << "expected : A B" << endl;


   MathSet<char> set6 = set4.difference(set2);
   cout << "testing difference on set ";
   for (int i = 0; i < set4.cardinality(); i++) {
       cout << set4.get(i) << " ";
   }
   cout << "and set ";
   for (int i = 0; i < set2.cardinality(); i++) {
       cout << set2.get(i) << " ";
   }
   cout << ": ";
   for (int i = 0; i < set6.cardinality(); i++) {
       cout << set6.get(i) << " ";
   }
   cout << "expected : A" << endl;


   return 0;
}

- 0 x G Microsoft Visual Studio Debug Console for testing create sets of characters create empty set size of set : 0 expected

let me know if you have any problem. thank you.

Add a comment
Know the answer?
Add Answer to:
PLEASE ANSWER THE FOLLOWING IN C++!! PLEASE READ THE QUESTION CAREFULLY!!! AS WELL AS WHOEVER ANSWERS...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Use C++ programing language Please don't use other Chegg solved solutions, make it new thanks. Assignment...

    Use C++ programing language Please don't use other Chegg solved solutions, make it new thanks. Assignment requirements Pre-requisite: Please read zyBook Chapter 6 Sets and understand the three operations of Sets (union, intersection, and difference) . You will be creating your custom Set (think a list of unique elements) class in C++ or a language of your choice. Please do NOT use STL, or any pre-defined library for this assignment. 1. The data type of the set collection: array (or...

  • Write a C++ program that takes two sets ’A’ and ’B’ as input read from the...

    Write a C++ program that takes two sets ’A’ and ’B’ as input read from the file prog1 input.txt. The first line of the file corresponds to the set ’A’ and the second line is the set ’B’. Every element of each set is a character, and the characters are separated by space. Implement algorithms for the following operations on the sets. Each of these algorithms must be in separate methods or subroutines. The output should be written in the...

  • This question has been answered before, but no codes given as the answer were right. please...

    This question has been answered before, but no codes given as the answer were right. please help. In the mathematical theory of sets, a set is defined as a collection of distinct items of the same type. In some programming languages, sets are built-in data types; unfortunately, this is not the case in C++. However, we can simulate a set using a one-dimensional array. Some operations can be performed on sets. We will consider three(3) of them: union, intersection and...

  • Please prove the following theorems using the provided axioms and definitions, using terms like s...

    Please prove the following theorems using the provided axioms and definitions, using terms like suppose, let..ect. Please WRITE CLEARLY AND TYPE IF YOU CAN. 1 Order Properties Undefined Terms: The word "point" and the expression "the point x precedes the point y" will not be defined. This undefined expression will be written x 〈 y. Its negation, "x does not precede y," will be written X y. There is a set of all points, called the universal set, which is...

  • MUST USE C++ PLEASE READ THE QUESTION CAREFULLY ADD COMMENTS AND EXPLAIN THE CODES PLEASE. Given...

    MUST USE C++ PLEASE READ THE QUESTION CAREFULLY ADD COMMENTS AND EXPLAIN THE CODES PLEASE. Given the following skeleton of an unsorted list class that uses an unsorted linked list: template < class ItemType > struct NodeType {                 ItemType item;                 NodeType* next; }; template < class ItemType > class UList { public:                 UList(); // default constrctor                 UList(const UList &x); // we implement copy constructor with deep copy                 UList& operator = (UList &x); // equal sign...

  • Answer the following questions for the method intersection () below: public Set intersection (Set s1, Set s2) //Effects: If s1 or s2 is null throw NullPointerException /I else return a (non nul) Set...

    Answer the following questions for the method intersection () below: public Set intersection (Set s1, Set s2) //Effects: If s1 or s2 is null throw NullPointerException /I else return a (non nul) Set equal to the intersection // of Sets s1 and s2 Characteristic: Validity of s1 -s1 has at least one element Characteristic: Relation between s1 and s2 s1 and s2 represent the same set -$1 is a subset of s2 - s2 is a subset of s1 $1...

  • JUST DO QUESTION 4 Université d'Ottawa Faculté de génie University of Ottawa Faculty of Engineeing École...

    JUST DO QUESTION 4 Université d'Ottawa Faculté de génie University of Ottawa Faculty of Engineeing École de science informatique et de génle électrique uOttawa School of Electrical Engineering and Computer Science Canada's universiry ELG 3126 RANDOM SIGNALS AND SYSTEMS Winter 2018 ASSIGNMENT 1 Set Theory (due at 11.30 AM Thusday, Jan. 18 in class) I. Your University of Ottaa stdent number has k distinct digits in it. State the set of t and all the subsets of this set that...

  • The definition we gave for a function is a bit ambiguous. For example, what exactly is a "rule"? ...

    The definition we gave for a function is a bit ambiguous. For example, what exactly is a "rule"? We can give a rigorous mathematical definition of a function. Most mathematicians don't use this on an everyday basis, but it is important to know that it exists and see it once in your life. Notice this is very closely related to the idea of the graph of a function. Definition 9. Let X and Y be sets. Let R-X × Y...

  • can you please prove the following theorem using the provided axioms and defintions. using terms like...

    can you please prove the following theorem using the provided axioms and defintions. using terms like suppose in a paragraph format. please write clearly or type if you can ! 1 Order Properties Undefined Terms: The word "point and the expression "the point z precedes the point y will not be defined. This undefined expression wil be written z < y. Its negation, "z does not precede y," will be written y. There is a set of all points, called...

  • I am having trouble with my C++ program.... Directions: In this lab assignment, you are to...

    I am having trouble with my C++ program.... Directions: In this lab assignment, you are to write a class IntegerSet that represents a set of integers (by definition, a set contains no duplicates). This ADT must be implemented as a singly linked list of integers (with no tail reference and no dummy head node), but it need not be sorted. The IntegerSet class should have two data fields: the cardinality (size) of the set, and the head reference to the...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT