Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

please write code in C++ programming language C++ programming language please. In this assignment, you are provided the code for a List class that has

please write code in C++ programming language

C++ programming language please.

In this assignment, you are provided the code for a List class that has the functions to setup/initialize the list, insert to the list, delete from the list and etc. Your task is to extend the main function of the code to form an array of List objects and insert elements (randomly generated integers) to each of these List objects and print the contents of the List objects.

The base address of the array of List objects is stored in a pointer named 'listArray' of type List.

The number of List objects (identified using the variable 'numLists') to be stored in the array is 5.

The number of integer elements per List object (identified using the variable 'listSize') is 10.

Each List object is to be filled up with randomly generated integers in the range of 1 ... maxValue, where maxValue = 50.

Each insertion has to be made at index 0 of the List object. For example, if the random integers 23 45 12 34 49 are to be inserted to a List object, the contents of the List object after each insertion (at index 0) looks like this:

23

45 23

12 45 23

34 12 45 23

49 34 12 45 23

After filling up all the List objects, print the contents of each of them.

After printing the contents of all the List objects, delete each List object and eventually clear up the space for the array of List objects.

Please Include:

1 - The entire .cpp file with the main function implemented as indicated.

2) A PDF document containing the following:

With the insertions occurring at index 0 of a List, determine the time complexity of inserting 'n' elements to every List object in an array of 'm' List objects. Show the pseudo code for the insertion step and analyze the time complexity.

3) A screenshot of the execution of your code.

#include

#include

#include

#include

#include

#include

using namespace std;

class List{

private:

int *array;

int maxSize;

int endOfArray;

public:

List(){}

List(int size){

maxSize = size;

array = new int[maxSize];

endOfArray = -1;

}

void SetUp(int size){

maxSize = size;

array = new int[maxSize];

endOfArray = -1;

}

bool isEmpty(){

if (endOfArray == -1)

return true;

return false;

}

void resize(int s){

int *tempArray = array;

array = new int[s];

for (int index = 0; index < min(s, maxSize); index++){

array[index] = tempArray[index];

}

maxSize = s;

delete[] tempArray;

}

void insertAtIndex(int insertIndex, int data){

// if the user enters an invalid insertIndex, the element is

// appended to the array, after the current last element

if (insertIndex > endOfArray+1 || insertIndex < 0)

insertIndex = endOfArray+1;

if (endOfArray == maxSize-1){

resize(maxSize+1);

//resize(2*maxSize);

}

for (int index = endOfArray; index >= insertIndex; index--)

array[index+1] = array[index];

array[insertIndex] = data;

endOfArray++;

}

void deleteList(){

delete[] array;

}

void PrintList(){

for (int index = 0; index <= endOfArray; index++)

cout << array[index] << " ";

cout << endl;

}

};

int main(){

using namespace std::chrono;

srand(time(NULL));

int numLists;

cout << "Enter the number of Lists: ";

cin >> numLists;

int listSize;

cout << "Enter list size (number of integer elements in a List): ";

cin >> listSize;

int maxValue;

cout << "Enter the maximum value for an integer: ";

cin >> maxValue;

// Create an array of numLists, each of size listSize

// Setup/Initialize the member variables of each of the List objects in the array

// Populate each List object of the array with values ranging from 1 to maxValue

// Every time an integer is to be inserted to a List object, the integer is inserted

// at index 0 of the List object.

// Print the contents of each List object

// Delete all the List objects and clear the space allocated for the array of List // objects

system("pause");

return 0;

}

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Put Your Data To Work 52 Tips And Techniques For Effectively Managing Your Database

Authors: Wes Trochlil

1st Edition

0880343079, 978-0880343077

More Books

Students also viewed these Databases questions

Question

What are Decision Trees?

Answered: 1 week ago

Question

What is meant by the Term Glass Ceiling?

Answered: 1 week ago