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,...
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...
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).
Implement a non-recursive reverse print of linked list using stack and the main function to test:...
Implement a non-recursive reverse print of linked list using stack and the main function to test: You will need to finish the printReversed_nonrecursive method in ch04.LinkedStack2 class, and the ch04.UseStack2 is the main function to test. public class LinkedStack2<T> extends LinkedStack<T> { private void revPrint(LLNode<T> listRef) { if (listRef != null) { revPrint(listRef.getLink()); System.out.println(" " + listRef.getInfo()); } } public void printReversed() { revPrint(top); } /* use stack to implement non-recursive reverse print */ public void printReversed_nonrecursive() { } public...
Write a function that counts the number of times a given integer occurs in a Linked...
Write a function that counts the number of times a given integer occurs in a Linked List. What is the time complexity of your algorithm? Justify your answer in python
Question 1: Write a method getSmallest(), which returns the smallest number in the linked list. Question...
Question 1: Write a method getSmallest(), which returns the smallest number in the linked list. Question 2: Write a member method getPosition(int entry) which returns the position of the entry is in the linked list. If the entry is not in the list, return -1. Please use C++ language for both questions, I only need functions.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT