Please use simple C++ knowledge and EXACTLY follow the instruction to write the
ID: 3816601 • Letter: P
Question
Please use simple C++ knowledge and EXACTLY follow the instruction to write the full code. Thank you!
Question 1 (40 points A set is a collection of unordered items that contains no duplicates. Sets are important in Mathematics and are useful in programming to keep track of distinct items such as the courses someone registered for, or a person's friends in a social network. Implement a class called Myset that represents a set of integers. Myset class will be composed of a singly linked list object as a data member. You should use the list implementation ILList) that we went through in class. The class should use two member variables: items: a singly linked list of integers siv: an integer variable that keeps track of the number of elements in the set The class should have the following functionalities: Function Prototype Description The default cons My set Creates an empty set. int getSize() const Returns the number of elements in the set. bool isEmpty() const Returns true if the set is empty, false otherwise. void add (int el Adds a new element elto the set. The element is added only if the set does not have elas a member. void remove (int el) Removes el from the set. void intersection (Myset& T, MySet& I) The intersection of this and T. The result will be stored in L I is the set containing the elements that are in both this and void Union (MySet& T, MySet& U) The union of this and T. The result will be stored in U. U is the set containing the elements that are in tbis or T, or both. U is a set, so there should be no duplicate items.Explanation / Answer
//MySet.h
#include<iostream>
#include<string>
using namespace std;
struct node
{
int element;
struct node *next;
};
typedef struct node Node;
class MySet
{
Node *head;
int size;
public:
MySet();
int getSize() const;
bool isEmpty() const;
void add(int el);
void remove(int el);
void intersection(MySet&T, MySet &I);
void Union(MySet&T, MySet &I);
void difference(MySet&T, MySet &I);
string toString();
int &operator[](int i);
};
------------------
//MySet.cpp
#include "MySet.h"
MySet::MySet()
{
head = NULL;
size = 0;
}
int MySet::getSize() const
{
Node *cur = head;
int count = 0;
while (cur != NULL)
{
cur = cur->next;
count++;
}
return count;
}
void MySet::add(int el)
{
Node *newNode, *cur = head;
bool duplicate = 0;
newNode = new Node;
newNode->element = el;
newNode->next = NULL;
if (head == NULL)
{
head = newNode;
}
else
{
while (cur->next != NULL)
{
if (el == cur->element)
{
duplicate = 1;
break;
}
cur = cur->next;
}
if (!duplicate)
{
cur->next = newNode;
}
}
}
bool MySet::isEmpty() const
{
if (head == NULL)
return true;
else
return false;
}
void MySet::remove(int el)
{
Node *cur = head, *prev = cur ,*next = NULL;
if (head != NULL)
{
if (head->element == el)
head = head->next;
while (cur != NULL)
{
if (cur->element == el)
{
break;
}
prev = cur;
cur = cur->next;
}
prev->next = cur->next;
delete cur;
}
}
int &MySet::operator[](int index)
{
Node *cur = head;
for (int i = 0; i < getSize(); i++)
{
if (index == i)
return cur->element;
cur = cur->next;
}
}
void MySet::intersection(MySet &T, MySet &I)
{
Node *cur = head;
//add element of this object
int j = 0;
for (int i = 0; i < getSize(); i++)
{
if (j < T.getSize())
{
if (cur->element == T[i])
I.add(cur->element);
}
cur = cur->next;
}
}
void MySet::Union(MySet &T, MySet &I)
{
Node *cur = head;
//add element of this object
for (int i = 0; i < getSize(); i++)
{
I.add(cur->element);
cur = cur->next;
}
//add element of T object to I
for (int i = 0; i < T.getSize(); i++)
{
if (I[i] != T[i])
I.add(T[i]);
}
}
void MySet::difference(MySet &T, MySet &I)
{
Node *cur = head;
//add element of this object
int j = 0;
for (int i = 0; i < getSize(); i++)
{
if (j < T.getSize())
if (cur->element!=T[j++])
I.add(cur->element);
cur = cur->next;
}
}
string MySet::toString()
{
string s;
Node *cur = head;
for (int i = 0; i < getSize(); i++)
{
s += cur->element + 48;
if (!((i + 1) >= getSize()))
{
s += ',';
s += ' ';
}
cur = cur->next;
}
return s;
}
----------------
//Driver to test set
#include"MySet.h"
int main()
{
MySet s;
MySet t;
s.add(1);
s.add(2);
t.add(3);
t.add(2);
MySet U,I,D;
s.Union(t, U);
cout << "Union: "<<U.toString() << endl;
s.intersection(t, I);
cout << "Intersection: " << I.toString() << endl;
s.difference(t, D);
cout << "Difference: " << D.toString() << endl;
return 0;
}
------------------------------------------------------------------
//output
Union: 1, 2, 3
Intersection: 2
Difference: 1