Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Q2 (70 pts). In this question, you will write your own container class named MyList similar to vector class in C++. Your class must

Q2 (70 pts). In this question, you will write your own container class named MyList similar to vector class

Q2 (70 pts). In this question, you will write your own container class named MyList similar to vector class in C++. Your class must be a template class which will contain its elements in an array named my Array. Since this class will be a template class, you should include the function definitions at the end of the header file. You can also check the Stack example in Chapter 9. You should keep the size and capacity of your list in two separate attributes named mySize and myCapacity. Therefore, your class will have three data members: one generic dynamic array (you should use pointer representation as given in the Stack example) and two integers. In the list, the elements will always be sorted in decreasing order. As the function members of your class write the following functions: a. (10 pts) Write a constructor that accepts one integer parameter as the capacity of your list, creates a new array with the given capacity, and stores new array in myArray. The constructor should also set myCapacity to the parameter value and mySize to 0. If the parameter is not a positive number, you should set the capacity as 10 and create the array with 10 elements. b. (5 pts) Write getters for size (getSize) and capacity (getCapacity). c. (15 pts) Write add function that accepts an element having generic type and adds this element to the appropriate place in myArray. Note that the elements must always be sorted. For instance, if the list elements are [12, 9, 7, 3] and the parameter of add function is 5, it will be added as the fourth element. The elements will be [12, 9, 7,5, 3] after addition. You should also update the size (mySize) accordingly. The function should return nothing. If the capacity is full, you should double the capacity by creating a new array, copy existing elements into new array, add the element into appropriate place, destroy old array, store the new array in myArray, update myCapacity. d. (5 pts) Write two functions getMin and get Max that returns the minimum element and the maximum element in the list, respectively. If there is no element in the list, throw an exception. e. (10 pts) Write remove function that accepts an element having generic type, searches it in the array and removes all occurrences of that element in the array. You should shift the elements after removal. For instance, if the elements are [12, 9, 7, 5, 5, 3], and the parameter of remove function is 5, the list elements will be [12, 9, 7, 3] after the removal. You should also update the size (mySize) accordingly. The function should return nothing. f. (10 pts) Write remove Max function that does not accept any parameter and removes the first element from the list. You should shift the remaining elements after removal. For instance, if the elements are [12, 9, 7, 5, 5, 3], the list elements will be [9, 7, 5, 5, 3] after calling removeMax function. You should also update the size (mySize)

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Introduction to Algorithms

Authors: Thomas H. Cormen, Charles E. Leiserson, Ronald L. Rivest

3rd edition

978-0262033848

More Books

Students also viewed these Programming questions