Question

In: Computer Science

Implement a function that returns the maximum number in a given unsorted linked list. For example,...

Implement a function that returns the maximum number in a given unsorted linked list.
For example, there is a linked list 3->5->1->10->9. The printMax() function in max.c should return the maximum number in the linked list, namely 10 in the example.

1. Implement max.c with the completed printMax() function.
2. Provide an explanation for your solution

#include <stdio.h>

typedef struct node {
int value;
struct node *next;
} node;

int printMax(node *first) {
// Your implementation
return 0;
}

int main(void) {
node nodes[5]; //enough to run our tests
int i,array[5] = {3,5,1,10,9};
for(i=0; i < sizeof(nodes)/sizeof(node)-1; i++) {
nodes[i].next = &nodes[i+1];
nodes[i].value = array[i];
}
nodes[i].value = array[4];
nodes[i].next = NULL;
printf("The maximum number is %d\n", printMax(&nodes[0]));
return 0;
}


Solutions

Expert Solution

#include <stdio.h>
typedef struct node {
   int value;
   struct node *next;
} node;

int printMax(node *first) {
   int max=first->value;
   while(first!=NULL){
       if(max<first->value)
           max=first->value;
       first=first->next;
   }
   return max;
}

int main(void) {
   node nodes[5]; //enough to run our tests
   int i,array[5] = {3,5,1,10,9};
   for(i=0; i < sizeof(nodes)/sizeof(node)-1; i++) {
       nodes[i].next = &nodes[i+1];
       nodes[i].value = array[i];
   }
   nodes[i].value = array[4];
   nodes[i].next = NULL;
   printf("The maximum number is %d\n", printMax(&nodes[0]));
   return 0;
}


Explanation:

1)take a variable max which tracks the max value in the array,
2)this max was intially loaded with the first node value,to compare with the remaining elements of the array.
3)here we are using while loop to traverse through each node.
4)for traversing purpose we use first pointer itself.
5)in the loop compare each value in the each node with max variable if it is more than max than max will replace with that value.
6)after each iteration fitst pointer move foreword towords end.
7) when first pointer points NULL it will stop.


Related Solutions

Given a doubly linked list in c++, how do I create a function that returns the...
Given a doubly linked list in c++, how do I create a function that returns the pointer to first node in the given pattern, For example, given mainList (a -> b -> c -> d) and sublist  (b -> c), our function should return a Node pointer that points to first node of the sublist in the mainList. If the pattern doesn't exist in the mainList, we should return a nullptr, there are multiple of the same sublist in the mainList,...
Given a doubly linked list in c++, how do I create a function that returns the...
Given a doubly linked list in c++, how do I create a function that returns the pointer to first node in the given pattern, For example, given mainList (a -> b -> c -> d) and sublist  (b -> c), our function should return a Node pointer that points to first node of the sublist in the mainList. If the pattern doesn't exist in the mainList, we should return a nullptr, there are multiple of the same sublist in the mainList,...
You are given a singly linked list. Write a function to find if the linked list...
You are given a singly linked list. Write a function to find if the linked list contains a cycle or not. A linked list may contain a cycle anywhere. A cycle means that some nodes are connected in the linked list. It doesn't necessarily mean that all nodes in the linked list have to be connected in a cycle starting and ending at the head. You may want to examine Floyd's Cycle Detection algorithm. /*This function returns true if given...
write the method “getMaxValue” that finds and returns the maximum value in an integer linked list....
write the method “getMaxValue” that finds and returns the maximum value in an integer linked list. If the list is empty, then it should return 0. use the provided code below public class Question03 { public class ListNode//public for testing purposes { public int data;//public for testing purposes public ListNode link;//public for testing purposes public ListNode(int aData, ListNode aLink) { data = aData; link = aLink; } } public ListNode head;//public for testing purposes public int getMaxValue() { //----------------------------------------------------------------------------------- //Write...
Problem 1: Unsorted arrays Given an array of integers that is unsorted, implement the following functions:...
Problem 1: Unsorted arrays Given an array of integers that is unsorted, implement the following functions: • myAdd ( ): add an integer d to the array; return 0 if the operation is successful; return a negative number otherwise. • search ( ): given an integer d, if d is found in the array, return the index of the cell containing d. Return a negative number otherwise (e.g., d is not found in the array). • myRemove ( ): Step...
Add a CountGroups method to the linked list class below (OurList). It returns the number of...
Add a CountGroups method to the linked list class below (OurList). It returns the number of groups of a value from the list. The value is passed into the method. A group is one or more values. Examples using strings: A list contains the following strings: one, one, dog, dog, one, one, one, dog, dog, dog, dog, one, one, dog, one    CountGroup(“one”) prints 4 groups of one's    CountGroup(“dog”) prints 3 groups of dog's Do not turn in the...
Add a CountGroups method to the linked list class below (OurList). It returns the number of...
Add a CountGroups method to the linked list class below (OurList). It returns the number of groups of a value from the list. The value is passed into the method. A group is one or more values. Examples using strings: A list contains the following strings: one, one, dog, dog, one, one, one, dog, dog, dog, dog, one, one, dog, one    CountGroup(“one”) prints 4 groups of one's    CountGroup(“dog”) prints 3 groups of dog's Do not turn in the...
Evaluate and write an algorithm to find the largest item in an unsorted singly linked list...
Evaluate and write an algorithm to find the largest item in an unsorted singly linked list with cells containing integers
Write a function which receives a list and returns a number. In the list, all numbers...
Write a function which receives a list and returns a number. In the list, all numbers have been repeated twice except one number that is repeated once. The function should return the number that is repeated once and return it.write a python program for this question. use main function.
In C++, type a method getSmallest(), which returns the smallest number in the following linked list....
In C++, type a method getSmallest(), which returns the smallest number in the following linked list. 8->4->6->7->5 (8 is the head).
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT