logo CodeStepByStep logo


Language/Type: C++ linked lists pointers
Author: Marty Stepp (on 2016/10/24)

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 (NULL if none)
Type your solution here:

This is a function problem. Write a C++ function as described. Do not write a complete program; just the function(s) above.

You must log in before you can solve this problem.

Log In

If you do not understand how to solve a problem or why your solution doesn't work, please contact your TA or instructor.
If something seems wrong with the site (errors, slow performance, incorrect problems/tests, etc.), please

Is there a problem? Contact a site administrator.

© Marty Stepp, all rights reserved.