Write a member function named stretch
that could be added to the ArrayIntList
class.
Your function accepts an integer k as a parameter and that replaces each integer in the original list with k copies of that integer.
For example, if an ArrayIntList
variable named list
stores this sequence of values:
{18, 7, 4, 4, 24, 11}
And the client makes the following call of list.stretch(3);
, the list should be modified to store the following values.
Notice that there are three copies of each value from the original list because 3 was passed as the parameter value.
{18, 18, 18, 7, 7, 7, 4, 4, 4, 4, 4, 4, 24, 24, 24, 11, 11, 11}
If the value of k is less than or equal to 0, the list should be empty after the call.
Remember that an array list has an internal "unfilled" array whose capacity might be larger than its size.
Note that this member function might require more capacity than your list's array currently has.
If so, you must handle this by resizing to a larger array if necessary.
You should not create any auxiliary arrays unless it is absolutely necessary to do so to solve the problem.
If the list's existing internal array already has enough capacity, you should perform the modification in place without using any auxiliary data structures.
Constraints:
Do not call any member functions of the ArrayList. For example, do not call add, insert, remove, or size.
You may, of course, refer to the private member variables inside the list.
Do not make assumptions about how many elements are in the list or about the list's capacity.
Do not use any auxiliary data structures to solve this problem (no vector, stack, queue, string, etc).
Do not leak memory; if you cease using any dynamically allocated (via new
) memory, free it.
Your code must run in no worse than O(N) time, where N is the length of the list.
Write the member function as it would appear in ArrayIntList.cpp
.
You do not need to declare the function header that would appear in ArrayIntList.h
.
Assume that you are adding this method to the ArrayIntList
class as defined below:
class ArrayIntList {
private:
int* elements; // array storing element data
int mysize; // number of elements in the array
int capacity; // array's length
public:
...
};