Does anyone know a good algorithm to find the maximum number in a linked list? I can not seem to come up with this code, and I don't see it posted any where.
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??? SEOT
LINKED LIST  ALGORITHM TO FIND A MAXIMUM # IN THE LIST
CAN'T FIGURE OUT THE LOGIC FOR MY LINKED LIST
Page 1 of 16 Replies  17054 Views  Last Post: 04 March 2008  10:48 AM
#1
LINKED LIST  ALGORITHM TO FIND A MAXIMUM # IN THE LIST
Posted 04 March 2008  10:16 AM
Replies To: LINKED LIST  ALGORITHM TO FIND A MAXIMUM # IN THE LIST
#2
Re: LINKED LIST  ALGORITHM TO FIND A MAXIMUM # IN THE LIST
Posted 04 March 2008  10:33 AM
I need to compare it to the other scores in the list. This algorithm does not seem to do that. Does anyone have any ideas??? Please help  SEOT
#3
Re: LINKED LIST  ALGORITHM TO FIND A MAXIMUM # IN THE LIST
Posted 04 March 2008  10:34 AM
hi,
can you post the complete function and the code from the structure/class Node please?
and please use the [code*] [/code*] tags (without the *)
can you post the complete function and the code from the structure/class Node please?
and please use the [code*] [/code*] tags (without the *)
This post has been edited by schnalf: 04 March 2008  10:35 AM
#4
Re: LINKED LIST  ALGORITHM TO FIND A MAXIMUM # IN THE LIST
Posted 04 March 2008  10:36 AM
SEOT, on 4 Mar, 2008  09:16 AM, said:
Does anyone know a good algorithm to find the maximum number in a linked list? I can not seem to come up with this code, and I don't see it posted any where.
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??? SEOT
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??? SEOT
You 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.
This post has been edited by Martyr2: 04 March 2008  10:38 AM
#5
Re: LINKED LIST  ALGORITHM TO FIND A MAXIMUM # IN THE LIST
Posted 04 March 2008  10:42 AM
I don't think I should post the code here. It is an assignment. I just want to know the pseudo code for how to solve it. I just want some ideas on how to go about finding the maximum number in a linked list. I can not figure out the logic. I just want the logic behind it if that makes sense.  SEOT
#6
Re: LINKED LIST  ALGORITHM TO FIND A MAXIMUM # IN THE LIST
Posted 04 March 2008  10:45 AM
SEOT, on 4 Mar, 2008  09:42 AM, said:
I don't think I should post the code here. It is an assignment. I just want to know the pseudo code for how to solve it. I just want some ideas on how to go about finding the maximum number in a linked list. I can not figure out the logic. I just want the logic behind it if that makes sense.  SEOT
Well I told you, your example there is a majority of it. All you have left there is to set the current node to the one pointed to next in line. Add that to the end of the loop and make sure you initialize max to the value of the head node score and you are 99% of the way there.
#7
Re: LINKED LIST  ALGORITHM TO FIND A MAXIMUM # IN THE LIST
Posted 04 March 2008  10:48 AM
Well I told you, your example there is a majority of it. All you have left there is to set the current node to the one pointed to next in line. Add that to the end of the loop and make sure you initialize max to the value of the head node score and you are 99% of the way there.
[/quote]
I thought I was on the right track, but I knew I was missing something, too! Thank you Martyr2...you were a great help to me!!!
[/quote]
I thought I was on the right track, but I knew I was missing something, too! Thank you Martyr2...you were a great help to me!!!
Page 1 of 1
