Exemple #1
0
LinkedBag<ItemType> LinkedBag<ItemType>::intersect(const LinkedBag& input) const{
	// make a new LinkedBag that will hold the intersect
	LinkedBag<ItemType> theIntersect;
	// traverse the existing linked list
	Node<ItemType>* callerPtr = headPtr;
	Node<ItemType>* paramPtr = input.headPtr;
	while (callerPtr != nullptr)
	{
		//cout<<"caller: "<<callerPtr->getItem()<<endl;
		while(paramPtr != nullptr){
		// compare item entries
		//cout<<"input: "<<paramPtr->getItem()<<endl;
			if(callerPtr->getItem() == paramPtr->getItem()){ //if there's a match
				//add to intersect
				theIntersect.add(callerPtr->getItem());
				// stop looking for this value 
				paramPtr = paramPtr->getNext();
				break; 
			}
			//advance to next item in paramPtr
			paramPtr = paramPtr->getNext();
		}
	//reset paramPtr
	paramPtr = input.headPtr;
	//advance to next item in callerPtr
	callerPtr = callerPtr->getNext();
	}
return theIntersect; 
}
Exemple #2
0
LinkedBag<ItemType> LinkedBag<ItemType>::difference(const LinkedBag& input) const{
	// make a new LinkedBag that will hold the difference
	LinkedBag<ItemType> theDiff;
	// traverse the existing linked list
	Node<ItemType>* callerPtr = headPtr;
	Node<ItemType>* paramPtr = input.headPtr;
	while (callerPtr != nullptr)
	{
		while(paramPtr != nullptr){
		// compare item entries
		if(callerPtr->getItem() == paramPtr->getItem()){
			//do nothing and stop looking 
			break;
		}
		//if we get to the end of input and don't find it.
		if(paramPtr->getNext() == nullptr){ 
			// items are not equivalent. 
			//add to difference
			theDiff.add(callerPtr->getItem());
			break;
		}
		//advance to next item in paramPtr
		paramPtr = paramPtr->getNext();
		}
	//reset paramPtr
	paramPtr = input.headPtr;
	//advance to next item in callerPtr
	callerPtr = callerPtr->getNext();

	}
return theDiff; 


}
Exemple #3
0
BagInterface<ItemType>* LinkedBag<ItemType>::intersection(const BagInterface<ItemType> &otherBag) const
{
    // create a new bag to return
    LinkedBag<ItemType>* newBag = new LinkedBag<ItemType>();
    int lcv;
    // create a temp copy of the current bag
    vector<ItemType> myVec = (*this).toVector();
    LinkedBag<ItemType> myBagCopy;
    for (lcv = 0; lcv < myVec.size(); lcv ++)
    {
        myBagCopy.add(myVec[lcv]);
    }
    // create a temp copy of the argument bag
    vector<ItemType> otherBagVec = otherBag.toVector();
    LinkedBag<ItemType> otherBagCopy;
    for (lcv = 0; lcv < otherBagVec.size(); lcv ++)
    {
        otherBagCopy.add(otherBagVec[lcv]);
    }
    // compare and add to the return bag
    for (lcv = 0; lcv < myVec.size(); lcv ++)
        if (myBagCopy.contains(myVec[lcv]) && otherBagCopy.contains(myVec[lcv]))
        {
            newBag->add(myVec[lcv]);
            myBagCopy.remove(myVec[lcv]);
            otherBagCopy.remove(myVec[lcv]);
        }
    return newBag;
}
Exemple #4
0
LinkedBag<ItemType> LinkedBag<ItemType>::trueDifference(const LinkedBag& input) const{
	// make a new LinkedBag that will hold the difference
	LinkedBag<ItemType> theDiff;
	// traverse the existing linked list
	Node<ItemType>* callerPtr = headPtr;
	Node<ItemType>* paramPtr = input.headPtr;
	int callerCount = 0;
	int inputCount = 0; 
	int diff = 0;
	int diffCount = 0;
	while (callerPtr != nullptr)
	{
		while(paramPtr != nullptr){
		// compare item entries
			if(callerPtr->getItem() == paramPtr->getItem()){
				//check to see if already in difference
				diffCount = theDiff.getFrequencyOf(callerPtr->getItem());
				// if already in difference, break
				if(diffCount > 0)
					break; 
				callerCount = getFrequencyOf(callerPtr->getItem());
				//cout<<"callerCount is: "<<callerCount<<endl;
				inputCount = input.getFrequencyOf(paramPtr->getItem());
				//cout<<"inputCount is: "<<inputCount<<endl; 
				diff = callerCount - inputCount;
				//cout<<"diff is: "<<diff<<endl;
				if (diff < 0)
					diff = 0; 
				if (diff > 0){
					for(int i=0; i<diff; i++)
						theDiff.add(callerPtr->getItem());
				}
				break;
	
			}		
			//if we get to the end of input and don't find it.
			if(paramPtr->getNext() == nullptr){ 
				// items are not equivalent. 
				//add to difference
				theDiff.add(callerPtr->getItem());
				break;
			}
		//advance to next item in paramPtr
		paramPtr = paramPtr->getNext();
		}
	//reset paramPtr
	paramPtr = input.headPtr;
	//advance to next item in callerPtr
	callerPtr = callerPtr->getNext();

	}
return theDiff; 
}
Exemple #5
0
LinkedBag<ItemType> LinkedBag<ItemType>::trueIntersect(const LinkedBag& input) const{
	// make a new LinkedBag that will hold the intersect
	LinkedBag<ItemType> theIntersect;
	// traverse the existing linked list
	Node<ItemType>* callerPtr = headPtr;
	Node<ItemType>* paramPtr = input.headPtr;
	int callerCount = 0;
	int inputCount = 0; 
	int intersect = 0;
	int sectCount = 0;
	while (callerPtr != nullptr)
	{
		//cout<<"caller: "<<callerPtr->getItem()<<endl;
		while(paramPtr != nullptr){
		// compare item entries
		//cout<<"input: "<<paramPtr->getItem()<<endl;
			if(callerPtr->getItem() == paramPtr->getItem()){ //if there's a match
			//check to see if already in intersect
				sectCount = theIntersect.getFrequencyOf(callerPtr->getItem());
				// if already in difference, break
				if(sectCount > 0)
					break; 
				callerCount = getFrequencyOf(callerPtr->getItem());
				//cout<<"callerCount is: "<<callerCount<<endl;
				inputCount = input.getFrequencyOf(paramPtr->getItem());
				//cout<<"inputCount is: "<<inputCount<<endl; 
				if(callerCount < inputCount) 
					intersect = callerCount;
				else if (inputCount < callerCount)
					intersect = inputCount;
				else
					// they're the same
					intersect = inputCount;
				//add to intersect
				for(int i=0; i<intersect; i++)
					theIntersect.add(callerPtr->getItem());
				// stop looking for this value 
				paramPtr = paramPtr->getNext();
				break; 
			}
			//advance to next item in paramPtr
			paramPtr = paramPtr->getNext();
		}
	//reset paramPtr
	paramPtr = input.headPtr;
	//advance to next item in callerPtr
	callerPtr = callerPtr->getNext();
	}
return theIntersect; 
}
Exemple #6
0
LinkedBag<ItemType> LinkedBag<ItemType>::bagUnion(const LinkedBag& input) const{
	// make a new LinkedBag that will hold the union
	// copy input values into the union bag using copy constructor 
	LinkedBag<ItemType> theUnion = LinkedBag(input);
	// add the caller values 
   Node<ItemType>* origChainPtr = headPtr;  // Points to nodes in original chain
   while (origChainPtr != nullptr) // only add if second chain isn't empty.
   {
      // Copy first node
		theUnion.add(origChainPtr->getItem());
		origChainPtr=origChainPtr->getNext();
	}
	return theUnion;
	}
Exemple #7
0
BagInterface<ItemType>* LinkedBag<ItemType>::difference(const BagInterface<ItemType> &otherBag) const

{
    int lcv;
    vector<ItemType> myVec = (*this).toVector();
    vector<ItemType> otherVec = otherBag.toVector();
    LinkedBag<ItemType>* newBag = new LinkedBag<ItemType>();
    for ( lcv = 0; lcv < myVec.size(); lcv ++ )
    {
        newBag->add(myVec[lcv]);
    }
    for (lcv = 0; lcv < otherVec.size(); lcv ++ )
    {
        newBag->remove(otherVec[lcv]);
    }
    return newBag;

}
Exemple #8
0
BagInterface<ItemType>* LinkedBag<ItemType>::Union(const BagInterface<ItemType> &otherBag) const

{
    LinkedBag<ItemType>* newBag = new LinkedBag<ItemType>();
    int lcv;
    vector<ItemType> myVec = this->toVector();
    vector<ItemType> otherVec = otherBag.toVector();
    
    for (lcv = 0; lcv < myVec.size(); lcv++)
    {
        newBag->add(myVec[lcv]);
    }
    for (lcv = 0; lcv < otherVec.size(); lcv++)
    {
        newBag->add(otherVec[lcv]);
    }

    return newBag;
}
void copyConstructorTester()
{
	LinkedBag<string> bag;
	string numbers[] = { "zero", "one", "two", "three", "four", "five" };
	for (int i = 0; i < bag.getCurrentSize(); i++)
	{
		cout << "Adding " << numbers[i] << endl;
		bool success = bag.add(numbers[i]);
		if (!success)
			cout << "Failed to add " << numbers[i] << " to the bag." << endl;
	}
	cout << bag;

	LinkedBag<string> copyOfBag = bag;
	cout << "Copy of bag: ";
	cout << copyOfBag;

	cout << "The copied bag: ";
	cout << bag;
}  // end copyConstructorTester
void bagTester() 
{
	LinkedBag<string> bag;
	cout << "Testing the Link-Based Bag:" << endl;
	cout << "isEmpty: returns " << bag.isEmpty() << "; should be 1 (true)" << endl;
	cout << bag;

	string numbers[] = { "one", "two", "three", "four", "five", "one" };
	cout << "Add 6 items to the bag: " << endl;
	for (int i = 0; i < sizeof(numbers)/sizeof(numbers[0]); i++)
	{
		bag.add(numbers[i]);
	}  // end for

	cout << bag;
	cout << "isEmpty: returns " << boolalpha << bag.isEmpty() << "; should be 0 (false)" << endl;
	cout << "getCurrentSize: returns " << bag.getCurrentSize() << "; should be 6" << endl;
	cout << "Try to add another entry: add(\"extra\") returns " << bag.add(string("extra")) << endl;
	cout << "contains(\"three\"): returns " << boolalpha << bag.contains(string("three")) << "; should be 1 (true)" << endl;
	cout << "contains(\"ten\"): returns " << bag.contains(string("ten")) << "; should be 0 (false)" << endl;
	cout << "getFrequencyOf(\"one\"): returns " << bag.getFrequencyOf(string("one")) << " should be 2" << endl;
	cout << "remove(\"one\"): returns " << boolalpha << bag.remove(string("one")) << "; should be 1 (true)" << endl;
	cout << "getFrequencyOf(\"one\"): returns " << bag.getFrequencyOf(string("one")) << " should be 1" << endl;
	cout << "remove(\"one\"): returns " << boolalpha << bag.remove(string("one")) << "; should be 1 (true)" << endl;
	cout << "remove(\"one\"): returns " << boolalpha << bag.remove(string("one")) << "; should be 0 (false)" << endl;
	cout << endl;

	cout << bag;

	cout << "After clearing the bag, ";
	bag.clear();

	cout << "isEmpty: returns " << bag.isEmpty() << "; should be 1 (true)" << endl;
}  // end bagTester