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 [... max Value]. The initial contents of the Integerlist are printed. The main function then calls the SwapMin MaxData(List) function and passes the IntegerList as the argument. In the SwapMin MaxDataList 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 (ie, the minimum and maximum values of these two nodes are then swapped. The final contents of the Integer List are printed after the call to the Swap MinMaxData(list) function. The Swap Min MaxDataList) function should be implemented in in time, where n is the number of clements in the list You would test run your code with max Value 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 dut values #include
#include #include #include #include setNextNodePtr(0); } Node* getHeadPtr(){ return headPtr; } bool isEmpty(){ if (headPtr->getNextNodePtr() == 0) return true; return false; } void insert(int data){ Node* currentNodePtr = headPtr- >getNextNodePtr(); Node* prevNodePtr = headPtr; while (currentNodePtr != 0){ prevNodePtr = currentNodePtr; currentNodePtr = currentNodePtr- >getNextNodePtr(); } Node* newNodePtr = new Node(); newNodePtr->setData(data); newNodePtr->setNextNodePtr(0); prevNodePtr->setNextNodePtr(newNodePtr); } void insertSortedOrder(int data){ // Implement the function // the data should get inserted at an appropriate index/location in the list // such that the list stays sorted after the insertion } void insertAtIndex (int insertindex, int data){ Node* currentNodePtr = headPtr- >getNextNodePtr(); Node* prevNodePtr = headPtr; int index = 0; while (currentNodePtr != 0){ if (index == insertindex) break; prevNodePtr = currentNodePtr; currentNodePtr = currentNodePtr- >getNextNodePtr(); index++; Node* newNodePtr = new Node(); newNodePtr->setData(data); newNodePtr- >setNextNodePtr(currentNodePtr); prevNodePtr->setNextNodePtr(newNodePtr); } int read(int readIndex) { Node* currentNodePtr = headPtr- >getNextNodePtr(); Node prevNodePtr = headPtr; int index = 0; while (currentNodePtr != 0){ if (index == readlndex) return currentNodePtr->getData(); prevNodePtr = currentNodePtr; currentNodePtr = currentNodePtr- >getNextNodePtr(); index++; } return -1; // an invalid value indicating // index is out of range prevNodePtr = currentNodePtr; currentNodePtr = currentNodePtr- >getNextNodePtr(); index++; } prevNodePtr- >setNextNodePtr(nextNodePtr); } void Iterative Print() { Node* currentNodePtr = headPtr- >getNextNodePtr(); while (currentNodePtr != 0){ cout getData() currentNodePtr = currentNodePtr- >getNextNodePtr(); } cout > maxValue; int listSize; cout > listSize; // srand(time(NULL)); srand( static_cast(time(nullptr))); using namespace std::chrono; int main(){ int maxValue; cout > maxValue; int listSize; cout > listSize; // srand(time(NULL)); srand( static_cast(time(nullptr))); using namespace std::chrono; List IntegerList; for (int i = 0; i