Provide the code for the IntSet class in the text-box below. An IntSet object st
ID: 3606674 • Letter: P
Question
Provide the code for the IntSet class in the text-box below. An IntSet object stores a set of unique integers and is further described as follows: The IntSet class must have at least two private instance variables: data which is an array of int and numData which is of type int. The IntSet class does not store duplicate data items; thus before a new data item is inserted, the data must be checked to determine if that value already exists in the set. The following public methods must be present in the class: . A constructor accepting a parameter of type int, which will set the maximum size of the set. The array will never be . boolean contains(int item)- Returns true if item appears in the list and false othe old more than this many elements. In the beginning, the set will have no elements int cardinality()-Return the number of data items in this set. . boolean add(int item) - Adds item to the list if it is not already in the set. This method returns true if item was successfully added, and false if not (, e·item was already in the set, or the set was full) IntSet union(IntSet other) Creates and returns a new IntSet containing all the elements of this set and the IntSet other. The maximum size of the new set is to be the sum of the cardinalities of this set and otherExplanation / Answer
#include<bits/stdc++.h>
using namespace std;
class IntSet
{
int max_size;
int *data;
int numData;
public:
IntSet(int max_size)
{
this->max_size=max_size;
data=new int[max_size];
numData=0;
}
bool contains(int item)
{
for(int i=0;i<numData;i++)
{
if(data[i]==item)
return true;
}
return false;
}
int cardinality()
{
return numData;
}
bool add(int item)
{
if(numData==max_size || contains(item))
return false;
data[numData]=item;
numData++;
}
IntSet* union_(IntSet other)
{
IntSet *new_intset=new IntSet(cardinality()+other.cardinality());
for(int i=0;i<cardinality();i++)
{
new_intset->add(data[i]);
}
for(int i=0;i<other.cardinality();i++)
{
new_intset->add(other.data[i]);
}
return new_intset;
}
void display()
{
for(int i=0;i<cardinality();i++)
{
cout<<(data[i])<<" ";
}
cout<<endl;
}
};
int main()
{
IntSet *first=new IntSet(10);
first->add(21);
first->add(18);
first->add(6);
first->add(18); //returns false
first->add(15);
first->add(1);
first->display();
IntSet *other=new IntSet(5);
other->add(20);
other->add(6);
other->add(5);
other->add(20);//returns false
other->display();
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.