LINKED LIST - ALGORITHM TO FIND A MAXIMUM # IN THE LIST Does anyone know a good
ID: 3630021 • Letter: L
Question
LINKED LIST - ALGORITHM TO FIND A MAXIMUM # IN THE LISTDoes anyone know a good algorithm to find the maximum number in a linked list? I think it should look something like this, but I don't know:
Node *transverse
transverse = head;
int max = 0;
while (transverse != NULL)
if(transverse.score> max) {
max = transverse.score;
}
}
I have this code in a function, and it does not seem to work for me. Does this make sense or should I have something else???
Explanation / Answer
Node *transverse transverse = head; int max = 0; while (transverse != NULL) if(transverse.score> max) { max = transverse.score; } } I have this code in a function ou are on the right track with your code there. The only comment I have is that you should be setting max to the value of head.score. Then traverse because your max may be lower than 0 if you have negative scores. But you are certainly thinking in the right direction. Just remember to change transverse each time to the value pointed to by the current node at the end of the loop..... transverse = transverse->next. Explore it.
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.