class List { public: List(); List(int insize); ~List(); int get(int index) const
ID: 3549833 • Letter: C
Question
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
Dear,
The following methods are added to make it complete:
void List::add(int value)
{
int *array = new int[10];
if( size >10)
{
for(int i=0;i<10;i++)
*(array+i)=*(listarray+i);
List(20);
for(int i=0;i<10;i++)
*(listarray+i)=*(array+i);
*(listarray+10)=value;
size++;
}
else
{*(listarray+size)=value;
size++;}
}
void List::remove(int loc)
{
//validate location
if(loc<0 ||loc>size)
cout<<"Invalid index!!"<<endl;
//copy all above down to fill in slots
else
{ for(int i=loc;i<size-1;i++)
*(listarray+i)=*(listarray+i+1);
size--;
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.