Write a function called findMax that prints the maximum value in a linked list.
ID: 3904317 • Letter: W
Question
Write a function called findMax that prints the maximum value in a linked list. To achieve this you will have to rely on the struct Node and class LinkedList defined below struct Node int key; Node *next; Node *previous; Node (int k, Node *n, Node p)t key k; next n; previousp; 1; class LinkedListt private: Node Shead; Node *search(int value): public: LinkedList(){head = NULL;}; void addNode(int value, int newVal); void printList) void findMax(); 3: You only have to implement findMax(.) as search(...), addNode(), and printList(. are already implemented for you in the backend Note: the function header should be void findMax() not void LinkedList::find Max(0Explanation / Answer
void findMax() { Node *temp = head; int max = 0; while(temp != NULL) { if(temp == head || temp->key > max) { max = temp->key; } temp = temp->next; } return max; }
Related Questions
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.