Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Fix MinMaxFunction Entire Code #include #include #include #include #include #include using namespace std; // implementing the dynamic List ADT using Linked List class Node {

Fix MinMaxFunction

image text in transcribedimage text in transcribed

Entire Code

#include #include #include

#include #include #include

using namespace std;

// implementing the dynamic List ADT using Linked List

class Node {

private: int data; Node* nextNodePtr;

public: Node() {}

void setData(int d) { data = d; }

int getData() { return data; }

void setNextNodePtr(Node* nodePtr) { nextNodePtr = nodePtr; }

Node* getNextNodePtr() { return nextNodePtr; }

};

class List {

private: Node* headPtr;

public: List() { headPtr = new Node(); headPtr->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 Node* current = headPtr->getNextNodePtr();

//the data should get inserted at an appropriate index/ location in the list if (!current) { insertAtIndex(0, data); return; }

int index = 0; //such that the lsit stay in order after insertion while (current) { if (data getData()) { insertAtIndex(index, data); return; }

if (!current->getNextNodePtr()) { insertAtIndex(index + 1, data); return; }

current = current->getNextNodePtr(); index++; }

}

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 == readIndex) return currentNodePtr->getData();

prevNodePtr = currentNodePtr; currentNodePtr = currentNodePtr->getNextNodePtr();

index++;

}

return -1; // an invalid value indicating // index is out of range

}

void modifyElement(int modifyIndex, int data) {

Node* currentNodePtr = headPtr->getNextNodePtr(); Node* prevNodePtr = headPtr; int index = 0;

while (currentNodePtr != 0) {

if (index == modifyIndex) { currentNodePtr->setData(data); return; }

prevNodePtr = currentNodePtr; currentNodePtr = currentNodePtr->getNextNodePtr();

index++; }

}

void deleteElement(int deleteIndex) {

Node* currentNodePtr = headPtr->getNextNodePtr(); Node* prevNodePtr = headPtr; Node* nextNodePtr = headPtr; int index = 0;

while (currentNodePtr != 0) {

if (index == deleteIndex) { nextNodePtr = currentNodePtr->getNextNodePtr(); break; }

prevNodePtr = currentNodePtr; currentNodePtr = currentNodePtr->getNextNodePtr();

index++; }

prevNodePtr->setNextNodePtr(nextNodePtr);

}

void IterativePrint() {

Node* currentNodePtr = headPtr->getNextNodePtr();

while (currentNodePtr != 0) { cout getData() getNextNodePtr(); }

cout

}

};

void SwapMinMaxData(List list) { Node* biggestMax = headPtr; Node* smallestMin = headPtr; Node* tempBig = biggestMax; Node* tempSmall = smallestMin; Node* currentNodePtr = headPtr->getNextNodePtr(); Node* prevNodePtr = headPtr; //Node* nextNodePtr = currentNodePtr->getNextNodePtr();

while (currentNodePtr != 0) { if (currentNodePtr->getData() > biggestMax->getData()) { tempBig = prevNodePtr; biggestMax = currentNodePtr; } if (currentNodePtr->getData() getData()) { tempSmall = prevNodePtr; smallestMin = currentNodePtr;

} currentNodePtr = currentNodePtr->getNextNodePtr(); prevNodePtr = prevNodePtr->getNextNodePtr(); } currentNodePtr->getData() = tempSmall->getNextNodePtr()->getData(); tempBig->getNextNodePtr()->getData() = tempSmall->getNextNodePtr()->getData(); tempSmall->getNextNodePtr()->getData() = currentNodePtr->getData(); }

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

cout

SwapMinMaxData(IntegerList);

cout

system("pause");

return 0; }

Google Docs Log in to Canvas u Unreal Engine 4 Co... Related image G Shopping Microsoft Word - CSC. 2 / 3 91% + 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 Integer List (an object of class List) and fills it up with random integers in the raiige [1...maxValue). The initial contents of the IntegerList are printed. The main function then calls the SwapMinMaxDataList) function and passes the IntegerList 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 SwapMinMaxDataList) function should be implemented in (n) time, where it is the number of elements 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 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 437 After min-max swap 13 11 9 30 23 46 712 437 Question 3 - 35 pts) We saw in class that a dynamic array-based implementation of the Lost ADT preferable when the insert operations to the list is repeatedly done at the end of the Lol, whereas the ingly linked listed implementation is preferable when the innert operations are done clone to the head node te the new node is inserted as the nee next to the header in this question, we will quantitatively estimate thes tradeoff and determine the average ne per insert the beginning of the list at the end of the list with both the dynamic anayan singly linked simplementation Type here to search o 2 Q2 SinglyLinkedList1 (Global Scope) 236 237 avoid Swapinata(List list) 238 Mode biggestMax = header 239 Nodet smallestin = headPtrs 240 Node temple - biggest Mass 241 Nodet te Small - smallestin 242 Node currentNodePtr - header->getextodetr(); 243 Node prevodePtr - headers 244 245 246 /Hodet nextNodePtr - currentNodePtr->getext Modetri 247 248 while (currentNodePtr - 0) { 249 if (currentNodePtr->getData() > biggest Max->getData()) { 230 templis - previodeptes 251 biggest Max - currentNodePtr 252 253 if (currentNodePtreetData() smallestin->getData) 254 tempSmall - prevtider 255 smallest in current adeptes 256 -257 -> currentNoder currentNoder->gethet Hodet): 250 prevNodePtr - prevetretext oder > 261 currentNodePtr->getData() - tea1-extBlodet tota petlixtNoder()->get() - textidepretat: tepall selecteer total) currencept that 3 365 Em 265 Chu TABS 749 10 Build-intellSense - x 4 Errors 10 Warnings 0 0 Messages Error List Entire Solution Search Error List Code E0020 F0137 E0137 E0137 Description identifier "header" is undefined expression must be a modifiable vale expression must be a modifiable value expression must be a modifiable value Project o Singlinkedin 02 SinglyLinked oz: SinglyLinked Singlinked 2 Singlinked to 218 Singlinkedis. 201 2 Linkedin 02 Soitto 20 + Add to See Error List Output U ED Google Docs Log in to Canvas u Unreal Engine 4 Co... Related image G Shopping Microsoft Word - CSC. 2 / 3 91% + 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 Integer List (an object of class List) and fills it up with random integers in the raiige [1...maxValue). The initial contents of the IntegerList are printed. The main function then calls the SwapMinMaxDataList) function and passes the IntegerList 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 SwapMinMaxDataList) function should be implemented in (n) time, where it is the number of elements 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 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 437 After min-max swap 13 11 9 30 23 46 712 437 Question 3 - 35 pts) We saw in class that a dynamic array-based implementation of the Lost ADT preferable when the insert operations to the list is repeatedly done at the end of the Lol, whereas the ingly linked listed implementation is preferable when the innert operations are done clone to the head node te the new node is inserted as the nee next to the header in this question, we will quantitatively estimate thes tradeoff and determine the average ne per insert the beginning of the list at the end of the list with both the dynamic anayan singly linked simplementation Type here to search o 2 Q2 SinglyLinkedList1 (Global Scope) 236 237 avoid Swapinata(List list) 238 Mode biggestMax = header 239 Nodet smallestin = headPtrs 240 Node temple - biggest Mass 241 Nodet te Small - smallestin 242 Node currentNodePtr - header->getextodetr(); 243 Node prevodePtr - headers 244 245 246 /Hodet nextNodePtr - currentNodePtr->getext Modetri 247 248 while (currentNodePtr - 0) { 249 if (currentNodePtr->getData() > biggest Max->getData()) { 230 templis - previodeptes 251 biggest Max - currentNodePtr 252 253 if (currentNodePtreetData() smallestin->getData) 254 tempSmall - prevtider 255 smallest in current adeptes 256 -257 -> currentNoder currentNoder->gethet Hodet): 250 prevNodePtr - prevetretext oder > 261 currentNodePtr->getData() - tea1-extBlodet tota petlixtNoder()->get() - textidepretat: tepall selecteer total) currencept that 3 365 Em 265 Chu TABS 749 10 Build-intellSense - x 4 Errors 10 Warnings 0 0 Messages Error List Entire Solution Search Error List Code E0020 F0137 E0137 E0137 Description identifier "header" is undefined expression must be a modifiable vale expression must be a modifiable value expression must be a modifiable value Project o Singlinkedin 02 SinglyLinked oz: SinglyLinked Singlinked 2 Singlinked to 218 Singlinkedis. 201 2 Linkedin 02 Soitto 20 + Add to See Error List Output U ED

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

More Books

Students also viewed these Databases questions