# LINKED LIST - ALGORITHM TO FIND A MAXIMUM # IN THE LIST

Page 1 of 1

## 6 Replies - 17752 Views - Last Post: 04 March 2008 - 10:48 AMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'http://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=45100&amp;s=9a1573411165627edddb3462450ad78a&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 SEOT

• New D.I.C Head

Reputation: 1
• Posts: 16
• Joined: 01-December 07

# LINKED LIST - ALGORITHM TO FIND A MAXIMUM # IN THE LIST

Posted 04 March 2008 - 10:16 AM

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

Is This A Good Question/Topic? 1

## Replies To: LINKED LIST - ALGORITHM TO FIND A MAXIMUM # IN THE LIST

### #2 SEOT

• New D.I.C Head

Reputation: 1
• Posts: 16
• Joined: 01-December 07

## 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
Was This Post Helpful? 0

### #3 schnalf

• D.I.C Head

Reputation: 6
• Posts: 129
• Joined: 09-February 08

## 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 *)

This post has been edited by schnalf: 04 March 2008 - 10:35 AM

Was This Post Helpful? 0

### #4 Martyr2

• Programming Theoretician

Reputation: 4867
• Posts: 13,190
• Joined: 18-April 07

## 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

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

Was This Post Helpful? 0

### #5 SEOT

• New D.I.C Head

Reputation: 1
• Posts: 16
• Joined: 01-December 07

## 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
Was This Post Helpful? 0

### #6 Martyr2

• Programming Theoretician

Reputation: 4867
• Posts: 13,190
• Joined: 18-April 07

## 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.

Was This Post Helpful? 0

### #7 SEOT

• New D.I.C Head

Reputation: 1
• Posts: 16
• Joined: 01-December 07

## 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!!!
Was This Post Helpful? 0

Page 1 of 1

 .related ul { list-style-type: circle; font-size: 12px; font-weight: bold; } .related li { margin-bottom: 5px; background-position: left 7px !important; margin-left: -35px; } .related h2 { font-size: 18px; font-weight: bold; } .related a { color: blue; }