I think I'm there, but I'm not sure if the insert() is correctly doubling the dynamically allocated memory. In an attempt in my insert() I tried to:
1 double the allocated memory (not sure if that was accomplished)
2 copy original values to the newArray
3 free original space
4 reassign pointer to original
The only thing I had to do with this program was to modify the insert() function to double the dynamic memory, which hopefully I did correctly, but I'm not sure. The rest of the program shouldn't need any modifications. Any tips would be great...Thanks!
P.S. please ignore cout statements in my member functions, I just used them to keep track of where I was at
#include <iostream>
#include <cassert>
#include <new>
using namespace std;
typedef int ElementType;
class List
{
public:
List(int maxSize = 1024);
~List();
List(const List & origList);
const List & operator=(const List & rightHandSide);
bool empty() const;
void insert(ElementType item, int pos);
void erase(int pos);
void display(ostream & out) const;
private:
int mySize; // current size of list
int myCapacity; // capacity of array
ElementType * myArray; // pointer to dynamic array
};
ostream & operator<< (ostream & out, const List & aList);
//--- Definition of class constructor
List::List(int maxSize)
: mySize(0), myCapacity(maxSize)
{
myArray = new(nothrow) ElementType[maxSize];
assert(myArray != 0);
cout << "CONSTRUCTOR\n";
}
//--- Definition of class destructor
List::~List()
{
cout << "DESTRUCTOR\n";
delete [] myArray;
}
//--- Definition of copy constructor
List::List(const List & origList)
: mySize(origList.mySize), myCapacity(origList.myCapacity)
{
//--- Get new array for copy
myArray = new(nothrow) ElementType[myCapacity];
if (myArray != 0) // check if memory available
//--- Copy origList's elements into this new array
for(int i = 0; i < mySize; i++)
myArray[i] = origList.myArray[i];
else
{
cerr << "*** Inadequate memory to allocate storage for list ***\n";
exit(1);
}
cout << "COPY CONSTRUCTOR\n";
}
//--- Definition of assignment operator
const List & List::operator=(const List & rightHandSide)
{
if (this != &rightHandSide) // check that not self-assignment
{
//-- Allocate a new array if necessary
if (myCapacity != rightHandSide.myCapacity)
{
delete[] myArray;
myCapacity = rightHandSide.myCapacity;
myArray = new(nothrow) ElementType[myCapacity];
if (myArray == 0) // check if memory available
{
cerr << "*Inadequate memory to allocate stack ***\n";
exit(1);
}
}
//--- Copy rightHandSide's list elements into this new array
mySize = rightHandSide.mySize;
for(int i = 0; i < mySize; i++)
myArray[i] = rightHandSide.myArray[i];
}
return *this;
}
//--- Definition of empty()
bool List::empty() const
{
return mySize == 0;
}
//--- Definition of display()
void List::display(ostream & out) const
{
for (int i = 0; i < mySize; i++)
out << myArray[i] << " ";
}
//--- Definition of output operator
ostream & operator<< (ostream & out, const List & aList)
{
aList.display(out);
return out;
}
void List::insert(ElementType item, int pos)
{
if (mySize == myCapacity)
{
cout << "im here at INSERT" << endl;
ElementType * newArray;
newArray = new(nothrow) ElementType[myCapacity * 2];
for(int i = 0; i < myCapacity; i++)
newArray[i] = myArray[i];
myCapacity *= 2;
delete [] myArray;
myArray = newArray;
}
if (pos < 0 || pos > mySize)
{
cout << "INSERT" << endl;
cerr << "*** Illegal location to insert -- " << pos
<< ". List unchanged. ***\n";
return;
}
// First shift array elements right to make room for item
cout << "INSERT" << endl;
for(int i = mySize; i > pos; i--)
myArray[i] = myArray[i - 1];
// Now insert item at position pos and increase list size
myArray[pos] = item;
mySize++;
}
//--- Definition of erase()
void List::erase(int pos)
{
if (mySize == 0)
{
cerr << "*** List is empty ***\n";
return;
}
if (pos < 0 || pos >= mySize)
{
cerr << "Illegal location to delete -- " << pos
<< ". List unchanged. ***\n";
return;
}
// Shift array elements left to close the gap
for(int i = pos; i < mySize; i++)
myArray[i] = myArray[i + 1];
// Decrease list size
mySize--;
}
int main()
{
int listLimit;
cout << "Enter max number of list elements: ";
cin >> listLimit;
List list1(listLimit);
for(int i = 0, n = 0; i < listLimit; i++, n++)
{
list1.insert(n,i);
}
cout << "number of elements: " << listLimit << endl;
cout << "elements: " << list1 << "\n\n";
int extra;
cout << "Enter a(n) extra element(s) and I will double the dynamic array size: ";
cin >> extra;
//GOOD TO HERE
list1.insert(10000,listLimit);
list1.insert(400, listLimit);
cout << "List1 " << list1 << endl;
system("pause");
return 0;
}