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(T 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 ARRAY_LIST_H
#define ARRAY_LIST_H
#include T* old = data;
data = temp;
delete[] old;
}
public:
// Default constructor. Starts with capacity of 1. Will be increased if necessary.
ArrayList(){
count =0;
capacity =1;
data = new T[capacity];
}
// Copy constructor. Needed because we store data on the heap
ArrayList(const ArrayList& other){
count = other.count;
capacity = other.capacity;
// data = other.data;
data = new T[capacity];
for (int i =0; i count; i++){
data[i]= other.data[i];
}
}
// Overloaded assignment operator. Needed because we store data on the heap
ArrayList& operator=(const ArrayList& other){
if (capacity == other.capacity){
// capacities match
count = other.count;
for (int i =0; i count; i++){
data[i]= other.data[i];
}
}
else{
// capacities are different
delete[] data;
capacity = other.capacity;
count = other.count;
data = new T[capacity]; for (int i =0; i count; i++){
data[i]= other.data[i];
}
}
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 append(T x){
data[count]= x;
count++;
if (count == capacity){
inflate();
}
}
void prepend(T 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& operator[](int index){
return data[index];
}
// 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& operator>(std::ostream& os, const ArrayList
Need help c + + Introduction user of the class

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Programming Questions!