IntSet IntSet: :unionWith(const IntSet& other IntSet) const { } T for (int i = 0; i < otherIntSet.used; ++i) { IntSet.data[i]); add (other }
void IntSet::resize(int new_capacity)
{
if (new_capacity < used)
new_capacity = used;
if (new_capacity < 1)
new_capacity = 1;
capacity = new_capacity;
int *newData = new int[capacity];
for (int i = 0; i < used; ++i)
newData[i] = data[i];
delete[] data;
data = newData;
}
IntSet::IntSet(int initial_capacity)
{
if (initial_capacity >= 1)
{
capacity = initial_capacity;
}
else
{
capacity = DEFAULT_CAPACITY;
}
}
IntSet::IntSet(const IntSet& src)
{
capacity = src.capacity;
used = src.used;
data = new int[capacity];
for (int i = 0; i < used; ++i)
data[i] = src.data[i];
}
IntSet::~IntSet()
{
delete[] data;
}
IntSet& IntSet::operator=(const IntSet& rhs)
{
if (this != &rhs) {
int *newData = new int[rhs.capacity];
for (int i = 0; i < rhs.used; ++i)
newData[i] = rhs.data[i];
delete[] data;
data = newData;
capacity = rhs.capacity;
used = rhs.used;
}
return *this;
}
int IntSet::size() const
{
return used;
}
bool IntSet::isEmpty() const
{
if (used == 0)
return true;
else
return false;
}
bool IntSet::contains(int anInt) const
{
for (int i = 0; i < used; i++) {
if (data[i] == anInt)
return true;
}
return false;
}
bool IntSet::isSubsetOf(const IntSet& otherIntSet) const
{
for (int i = 0; i < used; i++) { //Use the contains method of IntSet class to check whether the otherIntSet contains data[i]
if (otherIntSet.contains(data[i]) == 0) { // If it returns 0, then the other IntSet does not contain any of the elements of data. Thus, return False.
return false;
}
}
return true;
}
void IntSet::DumpData(ostream& out) const
{ // already implemented ... DON'T change anything
if (used > 0)
{
out << data[0];
for (int i = 1; i < used; ++i)
out << " " << data[i];
}
}
IntSet IntSet::unionWith(const IntSet& otherIntSet) const
{
for (int i = 0; i < otherIntSet.used; ++i) {
add(otherIntSet.data[i]);
}
}
IntSet IntSet::intersect(const IntSet& otherIntSet) const
{
IntSet interSet = (*this); // Create copy of invoking IntSet
for (int index = 0; index < size(); index++) { //Loop through each of the elements in the data array.
if(!otherIntSet.contains(data[index])){ //If the otherIntSet does not contain the data[index],
interSet.remove(data[index]); //then remove the data[index] using the remove method.
}
}
return interSet;
}
IntSet IntSet::subtract(const IntSet& otherIntSet) const
{
IntSet S3 = IntSet(1);
return S3;
}
void IntSet::reset()
{
used = 0;
delete[] data;
int *newData = new int[DEFAULT_CAPACITY];
data = newData;
capacity = 1;
}
bool IntSet::add(int anInt)
{
if (contains(anInt) == 0) {
if (used > capacity) // check if the array is not full
resize(int(1.5 * capacity) + 1);
data[used] = anInt;
used++;
return true; //if not full then return to data
}
return false;
}
bool IntSet::remove(int anInt)
{
bool alreadyFound = false;
if (contains(anInt) == 0) {
return false;
}
for (int i = 0; i < used; i++) {
if (data[i] == anInt) {
alreadyFound = true;
}
if (alreadyFound == true && i != used - 1) {
data[i] = data[i + 1];
}
}
used--;
return true;
}
bool operator==(const IntSet& is1, const IntSet& is2)
{
if (is1.isSubsetOf(is2) && is2.isSubsetOf(is1)) {
return true;
}
return false;
}
Trending now
This is a popular solution!
Step by step
Solved in 3 steps