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

CAN'T FIGURE OUT THE LOGIC FOR MY LINKED LIST

Page 1 of 1

6 Replies - 15917 Views - Last Post: 04 March 2008 - 10:48 AM Rate Topic: -----

#1 SEOT  Icon User is offline

  • New D.I.C Head

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

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

Post icon  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  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • 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  Icon User is offline

  • D.I.C Head

Reputation: 6
  • View blog
  • 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  Icon User is offline

  • Programming Theoretician
  • member icon

Reputation: 4361
  • View blog
  • Posts: 12,180
  • Joined: 18-April 07

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

Posted 04 March 2008 - 10:36 AM

View PostSEOT, 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  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • 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  Icon User is offline

  • Programming Theoretician
  • member icon

Reputation: 4361
  • View blog
  • Posts: 12,180
  • Joined: 18-April 07

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

Posted 04 March 2008 - 10:45 AM

View PostSEOT, 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  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • 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