Question: Question 2 - 30 pts) In this question, you will write a code/function to swap the minimum and maximum data values of the nodes in

 Question 2 - 30 pts) In this question, you will writea code/function to swap the minimum and maximum data values of thenodes in a singly linked list-based implementation of the List ADT. Youare given the code for the singly linked list-based implementation of theList ADT along with a main function that creates an IntegerList (anobject of class List) and fills it up with random integers in

Question 2 - 30 pts) In this question, you will write a code/function to swap the minimum and maximum data values of the nodes in a singly linked list-based implementation of the List ADT. You are given the code for the singly linked list-based implementation of the List ADT along with a main function that creates an IntegerList (an object of class List) and fills it up with random integers in the range [1...maxValue]. The initial contents of the IntegerList are printed. The main function then calls the Swap MinMaxData(List) function and passes the Integer List as the argument. In the SwapMinMaxData(List list) function, the parameter list is scanned once from the head node to the last data node and as part of this process, the addresses of the nodes with the minimum and maximum data values are identified. Using the addresses of the two nodes, the data (i.e., the minimum and maximum values) of these two nodes are then swapped. The final contents of the IntegerList are printed after the call to the SwapMinMaxData(List) function. The SwapMinMaxData(List) function should be implemented in O(n) time, where n is the number of elements in the list. You would test run your code with maxValue as 50 and listSize as 10 passed as inputs. Take a screenshot of the output. A sample output is shown below (7 and 46 are respectively the minimum and maximum data values): Enter the maximum value in the list: 50 Enter the size for the list: 10 Before min-max swap 13 11 9 30 23 7 46 12 43 7 After min-max swap 13 11 9 30 23 46 7 12 43 7 void setNextNodePtr (Nader nodcPtr #include Ninclude setext vodePtria); } Nade+ get HeadPtr return headPtr; ) baol isEmpty( if headPtr->getNextNodePtr) == 0) return true; return false; 1 void insert(int datal { Nodex currentNodePtr - leadPtr->getNextNodePtr(); Node prevNodePtr = headPtr while (currentNodePtr != X while (currentNodePtr ! prevodePtr = currentNodePtr; currentNodePtr currentNodepur->getfextNodeptr); 1 Nodex memadePtr = new Nade!); nchiode Persebataldata); newfodePtr-setNextNodePtra); prevNodePtr->setNextNodePtrinentoceptr); } vaid insert SortedOrder(int data){ // Implement the function // the data should get inserted at an appropriate index/location in the list 11 such that the list stays sorted after the insertion } void insertAtIndex(int insert Index, int datal { Nodc- currentNodePtr - headptr getNextNodePtr(); Nodex preyNodeptr - headPtr; int index = while (currentNodePtr != { if (index == insert Index) break; prevodePtr = currentNodePtr; currentNodePtr = currentNodePtr->getNextNadeptr); indext; Nodex newadePtr = new Nadel): newtodePtr->setData(data); newfodePtr->setNextiodeftrcurrentNodeptr); prevNodePtr->setNextNodePtrinellodeptr); } int read(int read Index) Noder currentNodePtr - headPtr->getNextNodePtr(); Nodex prevNodePtr = headPtr int index = ; while (currentNodePtr - if (index == read Index) return currentNodePtr->getData(); return currentNodePtr.getData(); prevNodePtr = currentNodePtr; currentNodePtr = currentNodePtr->getNextNodePtr!); indexu; } return -1; // an invalid value indicating // index is out of range } void modifyElement(int modifyIndex, int datal Node- currentNodePtr - headPtr getNextNodePtr(); Nodex preyNogetr - headPtr; int index = 0; whilc currentNodePtr - if (index == modifyIndex){ currentNodePtr->setData(data); return; prewedePtr = currentNodePtr; currentNodePtr = currentNodetr->getfvextNodeptr); index++; ) void deleteElement(int deleteIndexl{ Nodex currentNodePtr = headPtr->getNextNodePtr(); Node prevladePtr = headPtr; Node- nextNodePtr - headPtr; int Index = 0; while currentNodePtr != @}{ if (index == deleteIndex) ( nextNodePtr = currentNodePtr->getNextNodePtr(); break; } prevodePtr = currentNodePtr; currentNodePtr = currentNodePtr->getNextNodeftru); index: prevodsPtr setvextNodePtrinextNodePtr): vold IterativePrint Nodex currentNodePtr = headPtr->getNextNodePtr(; while (currentNodePtr - OH cout getData() getData() (time(nullptr)); using narespace std::chrono; List Integerlist; for (int i = 0; i int maint) int max Value: cout > naxValue; int listSize: cout > ListSize; 1/5.canditim (NULL); srand static_cast unsigned int>(time(nullptri)); using namespace std::chrono; List IntegerList; for (int i = 0; i

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!