logo CodeStepByStep logo


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

Write a member function named removeRange that could be added to the LinkedIntList class. The function takes two integer parameters, min and max, and removes all elements from the list whose values are between min and max, inclusive. It should also return a Boolean value indicating whether the list was modified by the call: true if it was, and false if not. Suppose a LinkedIntList variable named list stores the following values:

{4, 2, 1, 10, 15, 8, 7, 4, 20, 36, -3, 40, 5}

The call of list.removeRange(4, 20); would and change the list to store the following elements, and return true:

{2, 1, 36, -3, 40}

If the value of the max parameter is less than that of the min parameter, you should throw an integer exception. If the list is empty or does not contain any elements in the range of min to max, return false and leave the list unchanged.

Constraints: Do not call any methods of the LinkedIntList class. Do not construct any new ListNode objects in solving this problem (though you may create as many ListNode* pointer variables as you like). Do not modify the data field of any nodes; you must solve the problem by changing links between nodes. Do not leak memory; if you remove nodes from the list, free their associated memory. Do not use any auxiliary data structures to solve this problem (no array, vector, stack, queue, string, etc). Your code must run in no worse than O(N) time, where N is the length of the list. You should make no more than one pass over the list.

Write the member function as it would appear in LinkedIntList.cpp. You do not need to declare the function header that would appear in LinkedIntList.h. Write only your member function, not the rest of the class. Assume that you are adding this method to the LinkedIntList class as defined below:

class LinkedIntList {
    ListNode* front;   // nullptr for an empty list
    your code goes here;
Type your C++ solution code here:

This is a member function problem. Submit a member function that will become part of an existing C++ class. You do not need to write the complete class, just the member function described in the problem.

You must log in before you can solve this problem.

Log In

Need help?

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.