logo CodeStepByStep logo

removeAllThreshold

Language/Type: C++ linked lists pointers

Write a function named removeAllThreshold that accepts three parameters: a reference to a pointer to a ListNodeDouble representing the front of a linked list of real numbers, a real number value to remove, and a tolerance threshold. Your function should remove all values from the list that are within +/- threshold of the given value to remove. You must preserve the original order of the remaining elements of the list. For example, if a variable named front points to the front of a list containing the following values:

{3.0, 9.0, 4.2, 2.1, 3.3, 2.3, 3.4, 4.0, 2.9, 2.7, 3.1, 18.2}

Then the call of removeAllThreshold(front, 3.0, 0.3); would remove all values that are between 2.7 and 3.3 inclusive from the list, yielding the following values:

{9.0, 4.2, 2.1, 2.3, 3.4, 4.0, 18.2}

You may assume that the tolerance threshold passed is non-negative.

Constraints: Do not modify the data field of existing nodes; change the list by changing pointers only. Do not construct any new ListNodeDouble objects in solving this problem (though you may create as many ListNodeDouble* pointer variables as you like). Do not use any auxiliary data structures to solve this problem (no array, vector, stack, queue, string, etc).

Assume that you are using the ListNodeDouble structure as defined below:

struct ListNodeDouble {
    double data;         // value stored in each node
    ListNode* next;      // pointer to next node in list (nullptr if none)
}
Function: Write a C++ function as described, not a complete program.

You must log in before you can solve this problem.

Log In

Need help?

Stuck on an exercise? Contact your TA or instructor.

If something seems wrong with our site, please

Is there a problem? Contact us.