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
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; }
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; }