Question
Need the answer in C++ Modify the class ArrayList given below by using expandable arrays. That is, if the list is full when an item
Need the answer in C++
Modify the class ArrayList given below by using expandable arrays. That is, if the list is full when an item is being added to this list, the elements will be moved to a larger array. The new array should have twice the size of the original array.
Using the new class ArrayList, write a program to store 1,000 random numbers, each in the interval [0, 500]. The initial size of the array in the class should be set to 100. Print the numbers.
(BELOW IS THE CODE ArrayList.cpp, main.cpp, ArrayList.h)
ArrayList.cpp
/******************************
* Week 1 lab - exercise 1: *
* a simple ArrayList class *
*******************************/
#include
#include "ArrayList.h"
using namespace std;
/*
* Default constructor. Sets length to 0, initializing the list as an empty
* list. Default size of array is 20.
*/
ArrayList::ArrayList()
{
SIZE = 20;
list = new int[SIZE];
length = 0;
}
/*
* Destructor. Deallocates the dynamic array list.
*/
ArrayList::~ArrayList()
{
delete [] list;
list = NULL;
}
/*
* Determines whether the list is empty.
*
* Returns true if the list is empty, false otherwise.
*/
bool ArrayList::isEmpty()
{
return length == 0;
}
/*
* Prints the list elements.
*/
void ArrayList::display()
{
for (int i=0; i < length; i++)
cout << list[i] << " ";
cout << endl;
}
/*
* Adds the element x to the end of the list. List length is increased by 1.
*
* x: element to be added to the list
*/
void ArrayList::add(int x)
{
if (length == SIZE)
{
cout << "Insertion Error: list is full" << endl;
}
else
{
list[length] = x;
length++;
}
}
/*
* Removes the element at the given location from the list. List length is
* decreased by 1.
*
* pos: location of the item to be removed
*/
void ArrayList::removeAt(int pos)
{
if (pos < 0 || pos >= length)
{
cout << "Removal Error: invalid position" << endl;
}
else
{
for ( int i = pos; i < length - 1; i++ )
list[i] = list[i+1];
length--;
}
}
main.cpp
/******************************
* Week 1 lab - exercise 1: *
* a simple ArrayList class *
*******************************/
#include
#include "ArrayList.h"
using namespace std;
/*
* Program to test the ArrayList class.
*/
int main()
{
ArrayList myList;
if (!myList.isEmpty()) myList.removeAt(0);
else myList.add(-1);
//the list contains -1
myList.display();
for (int i = 0; i < 5; i++)
{
myList.add(rand()%100); //add some random numbers to the list
}
//the list contains 6 numbers; the first one is -1
myList.display();
if (!myList.isEmpty()) myList.removeAt(0);
else myList.add(-1);
//the list contains 5 numbers; -1 has been removed
myList.display();
return 0;
}
ArrayList.h
/******************************
* Week 1 lab - exercise 1: *
* a simple ArrayList class *
*******************************/
/*
* Class implementing an array based list.
*/
class ArrayList
{
public:
ArrayList ();
~ArrayList();
bool isEmpty();
void display();
void add(int);
void removeAt(int);
private:
int SIZE; //size of the array that stores the list items
int *list; //array to store the list items
int length; //amount of elements in the list
};
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started