I am creating a list class using a dynamic array (later I will use a linked list). I am having trouble coming up with a good Add() function. Any help would be appreciated.
Private data members:
Code:
private:
int* list;
int end;
Constructor:
Code:
DList::DList()
{
end = -1;
list = new int[SIZE];
}
Add member function:
Code:
void DList::Add(int item)
{
int* temp = new int[Length() * 2];
end++;
if(end == (Length() - 1))
{
for(int i = 0; i < Length() - 1; i++)
{
temp[i] = list[i];
}
list = temp;
delete temp;
}
list[end] = item;
}
Above the Length() function returns the current size of the array - so if end of the array is equal to the length the array should double in size... This is where I am having problems, I dont know if the way I use the temp array above is correct.
Thanks in advance..