Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Add the de?nition to functions add, get, and remove. class List { public: List()

ID: 3549872 • Letter: A

Question

 Add the de?nition to functions add, get, and remove.
class List { public:         List();         List(int insize);         ~List();         int get(int index) const;         void add(int value);         int getSize() const;         void remove(int index); private:         int *listarray;         int size; }; /*--------------------------Constructs and Destructs------------------------*/ List::List() {         size = 10;               listarray = new int[size]; } List::List(int insize):size(insize) {         listarray = new int[insize]; } List::~List() {         delete [] listarray;         size = 0; } /*--------------------------Get-------------------------*/ int List::getSize() const  {         return size; } /*----------------------Add your code here!!!! -------------* /*---- add---*/ /* adds a value to array at end.   If array is too small, make bigger (size you need +10) new array copy delete old array */ /*-----get---*/ /*return value at position given */ /*------remove----*/ /*Remove value at position Just zero if at end. If in middle, make zero and copy all above down to fill in slots */            

Explanation / Answer

#include<iostream>

using namespace std;


class List

{

public:

List();

List(int insize);

~List();

int get(int index) const;

void add(int value);

int getSize() const;

void remove(int index);

private:

int *listarray;

int size;

int ind;

};

/*--------------------------Constructs and Destructs------------------------*/

List::List()

{

size = 10;

listarray = new int[size];

ind = 0;

}

List::List(int insize):size(insize)

{

listarray = new int[insize];

ind = 0;

}

List::~List()

{

delete [] listarray;

size = 0;

ind = 0;

}

/*--------------------------Get-------------------------*/

int List::getSize() const

{

return size;

}

/*----------------------Add your code here!!!! -------------*

/*---- add---*/

/* adds a value to array at end.

If array is too small, make bigger (size you need +10) new array copy

delete old array

*/

void List::add(int value)

{

if(ind<size)

{

listarray[ind++] = value;

}

else

{

int* temp = new int[size];

std::copy(listarray, listarray + size, temp);

delete [] listarray;

listarray = new int[size+10];

std::copy(temp, temp + size, listarray);

size += 10;

listarray[ind++] = value;

}

}

/*-----get---*/

/*return value at position given

*/

int List::get(int index) const

{

if(index<ind)

return listarray[index];

else

return -1;

}

/*------remove----*/

/*Remove value at position

Just zero if at end.

If in middle, make zero and copy all above down to fill in slots

*/

void List::remove(int index)

{

if(index<ind)

{

cout<<index<<" "<<ind<<endl;

for(int i = index; i < ind;i++)

{

listarray[i] = listarray[i+1];

}

listarray[ind] = 0;

ind--;

}

}


int main()

{


List l(2); // list of size 2

// Adds 1, 2, 3

l.add(1);

l.add(2);

l.add(3);

// Prints the elements - 1,2,3

cout<<l.get(0)<<" "<<l.get(1)<<" "<<l.get(2)<<endl;

//Remove 1

l.remove(0);

// Prints 2,3,-1

cout<<l.get(0)<<" "<<l.get(1)<<" "<<l.get(2)<<endl;

// Add 1

l.add(1);

// Prints 2,3,1

cout<<l.get(0)<<" "<<l.get(1)<<" "<<l.get(2)<<endl;

// No element at pos 3 to remove

l.remove(3);

// Prints 2,3,1

cout<<l.get(0)<<" "<<l.get(1)<<" "<<l.get(2)<<endl;


}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote