Question: Part I. In-Lab Exercise (10 points) Submit the solution files of Exercises online before the due date/time. Upload your assignment using a file named List.cpp

Part I. In-Lab Exercise (10 points) Submit the solution files of Exercises online before the due date/time. Upload your assignment using a file named List.cpp and List.h. The objective of this assignment is to extend the List class. Read the code for the List class implemented using the dynamic array we discussed in class: List.h and List.cpp. Try to review how the List class works. Your Programming Task: You are asked to extend the List class, by adding a constructor to it and adding a member function that allows us to expand the capacity: (1) A constructor that allows initializing a new List from an array. Declaration: List(ElementType * array, int arraySize, int capacity); The constructor should initialize a newly declared List with capacity set to be the "capacity" parameter. The size of the list should be "arraySize and should contain the first arraySize elements in array "array". Example: int A[] = {1,4,2,5,4}; // This should create a List with a capacity of 20, and stores 5 elements: 1, 4, 2, 5, 4 List mylist(A,5,20); (2) A new member function to the List class: void ChangeCapacity(int); This member function should allow us to change the capacity of a List object. It should not change the elements of the List. For example, if list2 is a List object which stores 5 elements 1, 2, 3, 4, and 5, with capacity 5. Now list2 is full, but we can call list2.ChangeCapacity(50) to change the capacity to 50. Note that list2 is still holding 5 elements 1, 2, 3, 4, and 5, but we are now able to insert more elements into it. Important note: In C++, once an array is created, there is no way to modify its length afterward. So, the trick here is you will need to create a new dynamic array with the new capacity, then switch to this new array for the storage (by changing the myArrayPtr field to the address to this array). Also, since the elements of the List object are supposedly preserved after the expansion, you need to copy the elements from the old array to the new array. Finally, since the old array is no longer needed, we need to delete it. A main program to test your extended List class: InLab6ListDriver.cpp. Submit your modified List.cpp and List.h.

Code:

_______________________________________________

List.h

#include using namespace std; typedef int ElementType; // Now is //can change int to double,char,... class List { public: List(int maxSize = 1024); //constructor //Big Three: ~List(); // Destructor List(const List & orig); // Copy Constructor List & operator= (const List & orig); // Assignment operator unsigned size() const; bool empty() const; //check if empty void insert(ElementType item, int pos); //insertion void erase(int pos); //deletion void display(ostream & out) const; //display every item ElementType get(unsigned pos) const; int getCapacity(); private: int mySize; // current # of items in list int myCapacity; ElementType * myArrayPtr; //pointer to dynamic array };

____________________________________________

List.cpp

#include #include #include "list.h" #pragma warning(disable:6386) // disable the buffer overrun warning message using namespace std; void List::insert(ElementType item, int pos) { if (mySize == myCapacity) exit(1); if (pos < 0 || pos > mySize) return; // shift array elements right to make room for item for(int i = mySize; i > pos; i--) myArrayPtr[i] = myArrayPtr[i - 1]; // insert item at pos and increase list size myArrayPtr[pos] = item; mySize++; // dont forget this! } List::List(int maxSize){ mySize = 0; myCapacity = maxSize; myArrayPtr = new ElementType[maxSize]; } List::~List(){ delete [] myArrayPtr;} List::List(const List & origList) { mySize = origList.mySize; myCapacity = origList.myCapacity; myArrayPtr = new ElementType[myCapacity]; for(int i = 0; i < mySize; i++) myArrayPtr[i] = origList.myArrayPtr[i]; } List & List::operator=(const List & origList) { if (this != & origList) // check for list = list { delete[] myArrayPtr; mySize = origList.mySize; myCapacity = origList.myCapacity; myArrayPtr = new ElementType[myCapacity];

_____________________________________

Driver code:

#include #include "list.h" using namespace std; int main() { int orig[] = {6,3,5,1,8,4,7}; List list1(orig, 7, 50); cout << "list1 created from array: " << endl; list1.display(cout); cout << endl; int orig2[] = {1,5,15,23,3}; List list2(orig2, 5, 5); cout << "list2 created from array: " << endl; list2.display(cout); cout << endl; cout << "Capacity of list2 is " << list2.getCapacity() << endl; cout << "Change the capacity of list 2 to 50." << endl; list2.ChangeCapacity(50); cout << "After the change of capacity, list2 becomes: " << endl; list2.display(cout); // Should be unchanged, since we change the capacity, not the contents cout << endl; cout << "Inserting more elements to the expanded list2." << endl; for(int i=0; i<45; i++){ list2.insert(i,0); } cout << "After the insertion, list2 becomes: " << endl; list2.display(cout); // Should be 44 43 42 41 40 39 38 37 36 35 34 33 32 31 30 29 28 27 26 25 24 23 22 21 20 19 18 17 16 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0 1 5 15 23 3 cout << endl; }

_________________________________

please provide the code im supposed to put

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!