Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

The function removeAt of the class arrayListType removes an element from the lis

ID: 440657 • Letter: T

Question

The function removeAt of the class arrayListType removes an element from the list by shifting the elements of the list. However, if the element to be removed is at the beginning ofthe list and the list is fairly large, it could take a lot of computer time. Because the list elements are in no particular order, you could simply remove the element by swapping the last element of the list with the item to be removed and reducing the length of the list. Rewrite the definition ofthe function removeAt using this technique. Add the function min to the class arrayListType to return the smallest element of the list. Also, write the definition of the function min and a program to test this function. Add the function max to the class arrayListType to return the largest element of the list. Also, write the definition of the function max and a program to test this function.

Explanation / Answer

You can use a priority queue for this purpose. When building a queue using an array, you can make a cyclic "pointer" to the head of the array, that you can use to retrieve the top. Popping from the array is simply done by increasing this cyclic pointer. Maintain an int variable: top, and once you need to pop an element do top = (top + 1) % items_.length Retrieving the head is simple using items_[top]. Make sure you are guarding against popping elements that are not there (popping from an empty array). You will probably also need to maintan a size variable for the queue's size. Cheers! Please rate :)

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote