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:
Constructor:Code:private: int* list; int end;
Add member function:Code:DList::DList() { end = -1; list = new int[SIZE]; }
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.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; }
Thanks in advance..



LinkBack URL
About LinkBacks


