Question

In: Computer Science

Assume you are in main.cpp define a function that is NOT SCOPED to any class. It...

Assume you are in main.cpp define a function that is NOT SCOPED to any class. It is NOT a member of the ListInterface nor LinkedList classes. Your function should accept two lists. You will remove all values from the first list that are present in the second list. Example:

Lists before call:                                    Lists after call:

target: A, B, C, A, D, C, E, F, B, C, A                  target: A, A, D, E, F, A

toRemove: C, B, Q, Z                                   toRemove: C, B, Q, Z

template <typename T>

void removeHelper(ListInterface &target, const ListInterface&toRemove)

{

//your code below

Solutions

Expert Solution

Here's the definition of the function removeHelper(): (in C++)

Assumption: Since no info is given in the problem regarding ListInterface, I've assumed that it's a struct of below type for simplicity purpose (you can modify according to your needs):

template<typename T>
struct ListInterface
{
    T val;
    ListInterface *next;
};

removeHelper()'s definition:

template<typename T>
void removeHelper(ListInterface<T> &target, const ListInterface<T> &toRemove) {
    
    // hashmap (using this would tell us if a value in the target list is present in toRemove list or not in O(1) time)
    unordered_map<T, int> toBeRemoved;

    ListInterface<T> prev = NULL, save;

    // assumption: val is the data item of type T (for generalising purpose)  & next is the pointer pointing to the next node in the list

    // adding all the values to be removed into the hashmap
    while (toRemove) {
        toBeRemoved[toRemove -> val] = 1;
        toRemove = toRemove -> next;
    }

    // iterate through target list
    while (target) {

        // if the current value in target is one of the values in the map then remove it
        if (toBeRemoved[target -> val]) {
            
            // if previous node exists
            if (prev)
                prev -> next = target -> next;

            // delete the current node
            save = target;
            target = target -> next;
            save -> next = NULL;
            delete save;
            continue;
        }

        // update prev and target pointers
        prev = target;
        target = target -> next;
    }


}

Hope this helps. For any doubt, please reach out, I'd be happy to help :)

If it worked for you, please give an upvote for this answer, I'd appreciate that! :)

Cheers!


Related Solutions

3 files cvehicle.h -- a partially filled-out class declaration for the CVehicle class main.cpp -- the...
3 files cvehicle.h -- a partially filled-out class declaration for the CVehicle class main.cpp -- the main module that creates and manipulates CVehicle objects cars.dat -- a text file that contains name data for the main module 4th file is cvehicle.cpp and it needs to be created from scratch, and cvehicle.h needs to be filled in This was the test drive: carOne = Hyundai Sonata carTwo = Hyundai Sonata carThree = Enter the make and model of a vehicle: toyota...
(1) Create three files to submit. ContactNode.h - Class declaration ContactNode.cpp - Class definition main.cpp -...
(1) Create three files to submit. ContactNode.h - Class declaration ContactNode.cpp - Class definition main.cpp - main() function (2) Build the ContactNode class per the following specifications: Parameterized constructor. Parameters are name followed by phone number. Public member functions InsertAfter() (2 pts) GetName() - Accessor (1 pt) GetPhoneNumber - Accessor (1 pt) GetNext() - Accessor (1 pt) PrintContactNode() Private data members string contactName string contactPhoneNum ContactNode* nextNodePtr Ex. of PrintContactNode() output: Name: Roxanne Hughes Phone number: 443-555-2864 (3) In main(),...
(1) Create three files to submit: ItemToPurchase.h - Class declaration ItemToPurchase.cpp - Class definition main.cpp -...
(1) Create three files to submit: ItemToPurchase.h - Class declaration ItemToPurchase.cpp - Class definition main.cpp - main() function Build the ItemToPurchase class with the following specifications: Default constructor Public class functions (mutators & accessors) SetName() & GetName() (2 pts) SetPrice() & GetPrice() (2 pts) SetQuantity() & GetQuantity() (2 pts) Private data members string itemName - Initialized in default constructor to "none" int itemPrice - Initialized in default constructor to 0 int itemQuantity - Initialized in default constructor to 0 (2)...
Using C++ 1. Create a main function in a main.cpp file. The main function should look...
Using C++ 1. Create a main function in a main.cpp file. The main function should look as follows int main() {return 0;} 2. Create an array. 3. Ask user to enter numbers in size of your array. 4. Take the numbers and store them in your array. 5. Go through your array and add all the numbers. 6. Calculate the average of the numbers. 7. Display the numbers, sum and average.
Implement the following functions. Each function deals with null terminated C-strings. You can assume that any...
Implement the following functions. Each function deals with null terminated C-strings. You can assume that any char array passed into the functions will contain valid, null-terminated data. Your functions must have the signatures listed below. 1. This function returns the last index where the target char can be found in the string. it returns -1 if the target char does not appear in the string. For example, if s is “Giants” and target is ‘a’ the function returns 2. int...
Implement the following functions. Each function deals with null terminated C-strings. You can assume that any...
Implement the following functions. Each function deals with null terminated C-strings. You can assume that any char array passed into the functions will contain valid, null-terminated data. Your functions must have the signatures listed below. 1. This function returns the last index where the target char can be found in the string. it returns -1 if the target char does not appear in the string. For example, if s is “Giants” and target is ‘a’ the function returns 2. int...
Define a class called Goals that has the following requirements in c++: a function called set...
Define a class called Goals that has the following requirements in c++: a function called set that takes 3 int parameters that are goals for "fame", "happiness" and "money". The function returns true and saves the values if they add up to exactly 60, and returns false otherwise (you may assume the parameters are not negative) a functions satisfies that takes 3 int parameters and returns true if each parameter is at least as large as the saved goal, false...
Is there any relationship between returns to scale and economies of scale? Assume a production function...
Is there any relationship between returns to scale and economies of scale? Assume a production function q = 100(K^0.7*L^0.3), where K is capital and L is labor. Derive the marginal product of labor and the marginal product of capital. Show that the marginal product of labor is decreasing (hint: beginning with K = 2, and L = 50)
Define a class template called genericStack for storing any data type in a static stack. Your...
Define a class template called genericStack for storing any data type in a static stack. Your class template must include a constructor, a destructor, push, pop, peek, isFull, and isEmpty functions (no display function is required). Write a simple main function in your program that demonstrates the class template with a stack of strings.
URGENT: USING C++: You are given a main.cpp and a Schedule.h file. You are to write...
URGENT: USING C++: You are given a main.cpp and a Schedule.h file. You are to write a Schedule.cpp file that implements the missing pieces from Schedule.h. // ************************************ // ************* main.cpp ************ // ************************************ #include "Schedule.h" void main() { Schedule s1(10); s1.addEntry(1,20,"Feed Cat"); s1.addEntry(2,40,"Feed Dog"); s1.addEntry(2,50,"Walk Dog"); s1.addEntry(4,0, "Fix Dinner"); s1.addEntry(5,15,"Eat Dinner"); s1.printSchedule(); Schedule s2(s1); // Note this uses the copy constructor cout << endl << "Output from s2 " << endl; s2.printSchedule(); } // ********************************************** // ************* Schedule.h ******************...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT