Question: Based on the book data abstraction and problem solving with c++ Extend bag ADT by: Add the methods union, intersection, and difference to the bag
Based on the book data abstraction and problem solving with c++
Extend bag ADT by:
Add the methods union, intersection, and difference to the bag ADT. The union of two bags is a new bag containing the combined contents of the original two bags. The intersection of two bags is a new bag containing the entries that occur in both of the original two bags. The difference of two bags is a new bag containing the entries that would be left in one bag after removing those that also occur in the second.
- Implement union method for class ArrayBag.
- Implement intersect method for class ArrayBag.
- Imlplement difference method for class ArrayBag.
/** Implementation file for the class ArrayBag. @file ArrayBag.cpp */
#include "ArrayBag.h"
template
ArrayBag
: itemCount(0), maxItems(DEFAULT_CAPACITY)
{
} // end default constructor
template
bool ArrayBag
{
bool hasRoomToAdd = (itemCount < maxItems);
if (hasRoomToAdd)
{
items[itemCount] = newEntry;
itemCount++;
} // end if
return hasRoomToAdd;
} // end add
/*The method toVector . The method toVector in our initial core group gets the entries that
are in a bag and returns them to the client within a vector.
A simple loop within toVector adds the bags entries to this vector.
*/
template
vector
{
vector
for (int i = 0; i < itemCount; i++)
bagContents.push_back(items[i]);
return bagContents;
} // end toVector
/*The methods getCurrentSize and isEmpty.The last two methods in
our core group have the following straightforward de nitions :*/
template
int ArrayBag
{
return itemCount;
} // end getCurrentSize
template
bool ArrayBag
{
return itemCount == 0;
} // end isEmpty
/*
template
bool ArrayBag
{
return false; // STUB
} // end remove
//A stub for the void method clear could be
*/
template
bool ArrayBag
{
int locatedIndex = getIndexOf(anEntry);
bool canRemoveItem = !isEmpty() && (locatedIndex > -1);
if (canRemoveItem) {
itemCount-- ;
items[locatedIndex] = items[itemCount];
} // end if
return canRemoveItem;
} // end remove
template
void ArrayBag
{
itemCounter = 0;// STUB
} // end clear
//Note that if you plan to call a stub within your test program, the stub sh
//ould report that it was invoked by displaying a message
template
int ArrayBag
{
bool found = false;
int result = -1;
int searchIndex = 0;
// If the bag is empty, itemCount is zero, so loop is skipped
while (!found && (searchIndex < itemCount))
{
if (items[searchIndex] == target)
{ found = true; result = searchIndex;
} else
{
searchIndex++;
} // end if
} // end while
return result;
} // end get IndexO
template
int ArrayBag
{
int frequency = 0; int curIndex = 0; // Current array index
while (curIndex < itemCount) {
if (items[curIndex] == anEntry)
{ frequency++;
} // end if
curIndex++; // Increment to next entry
} // end while return frequency;
return frequency;
} // end getFrequencyOf
template
bool ArrayBag
{
bool found = false;
int curIndex = 0; // Current array index
while (!found && (curIndex < itemCount)) {
if (anEntry == items[curIndex])
{ found = true;
} // end if
curIndex++; // Increment to next entry
} // end while
return found;
} // end contains
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
