Question: Need help c + + Introduction user of the class can insert new data into the list at the end. The list will automatically inflate
Need help c
Introduction
user of the class can insert new data into the list at the end. The list will automatically inflate itself, which doubles its capacity, if needed.
Tasks
Implement the following methods in ArrayList.h:
void prepend value;
The data element given by value should be inserted at the beginning of the list, as opposed to the end which is the way append works
T removeFirst
This method should remove the first data element from the list, and should return it
T removeLast
This method should remove the last data element from the list, and should return it
Note
For the removeFirst and removeLast functions above, if the removal procedure results in a situation where we are using "too much space", the storage capacity should be decreased deflated
Assume that "too much space" is defined as the capacity is strictly greater than twice the number of elements in the list. That is to say, if we were to cut the capacity in half, we would still have space available to insert
an element.
Therefore, our policy will be anytime we detect that we are using less than half the capacity, we should call our deflate function, which halves the capacity while preserving the data. #ifndef ARRAYLISTH
#define ARRAYLISTH
#include T old data;
data temp;
delete old;
public:
Default constructor. Starts with capacity of Will be increased if necessary.
ArrayList
count ;
capacity ;
data new Tcapacity;
Copy constructor. Needed because we store data on the heap
ArrayListconst ArrayList& other
count other.count;
capacity other.capacity;
data other.data;
data new Tcapacity;
for int i ; i count; i
datai other.datai;
Overloaded assignment operator. Needed because we store data on the heap
ArrayList& operatorconst ArrayList& other
if capacity other.capacity
capacities match
count other.count;
for int i ; i count; i
datai other.datai;
else
capacities are different
delete data;
capacity other.capacity;
count other.count;
data new Tcapacity; for int i ; i count; i
datai other.datai;
return this;
Appends a value to the end of the list
We guarantee that there is always space to add one more
But after we have added that one, we may have run out of space
The inflate function increases the capacity if necessary
void appendT x
datacount x;
count;
if count capacity
inflate;
void prependT value
Your code here...
T removeFirst
Your code here
return T;
T removeLast
Your code here
return T;
This is so we are able to access and manipulate read and write any element in a valid position in the list
T& operatorint index
return dataindex;
Returns the number of elements in the list not the current capacity
int size const
return count;
Returns the currently available capacity of the list
int getCapacity const
return capacity;
Destructor, responsible for releasing the memory used on the heap
~ArrayList
delete data;
Some friendship declarations here, for the overloaded operator
and for Unit Testing purposes. We want the test program to have access to everything
friend std::ostream& operatorstd::ostream& os const ArrayList
Step by Step Solution
There are 3 Steps involved in it
1 Expert Approved Answer
Step: 1 Unlock
Question Has Been Solved by an Expert!
Get step-by-step solutions from verified subject matter experts
Step: 2 Unlock
Step: 3 Unlock
