#include "list.h" #include using namespace std; List::List() { listSize = 10; listPointer = new int[listSize]; position = 0; } List::List(int size) { listSize = size; listPointer = new int[size]; position = 0; } void List::PrintList() { for(int i = 0; i < listSize; i++) { cout << listPointer[i] << endl; } } void List::AddItem(int n) { if(position + 1 > listSize) // Increase the list once max size is exceeded { int * newPointer; // Create resized list int newSize = (listSize * .50) + listSize; // makes the size 50% larger cout << "Resizing data structure to: " << newSize << endl << endl; newPointer = new int[newSize]; for(int i = 0; i <= listSize; i++) // Assign data to larger data structure { newPointer[i] = listPointer[i]; } delete listPointer; // Delete all data structure listPointer = newPointer; // Assign the pointer listSize = newSize; // Update size } listPointer[position] = n; position++; } #ifndef LIST_H #define LIST_H class List { public: List(); List(int); void AddItem(int); void PrintList(); private: int listSize; int position; int * listPointer; }; #endif #include #include "list.h" using namespace std; void main() { int size; cout << "Please enter the initial list size: "; cin >> size; /* Fill up new list and print */ if(size <= 0) { cout << "List size cannot be zero, use default size of 10." << endl; size = 10; } List newList(size); for(int i = 0; i < size; i++) { newList.AddItem(i); } newList.PrintList(); cout << endl; /* Add 5 more items to already full list, to check for resizing */ cout << "Adding 5 items: " << endl; for(int i = 10; i < 15; i++) { newList.AddItem(i); } newList.PrintList(); } /* Program Output */ /* Please enter the initial list size: 10 0 1 2 3 4 5 6 7 8 9 Adding 5 items: Resizing data structure to: 15 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 Press any key to continue . . . */