# Finding and listing the Highest and Lowest and lowest in a linked list

Page 1 of 1

## 13 Replies - 1195 Views - Last Post: 21 February 2009 - 09:43 PMRate 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=88187&amp;s=3e5131db1327c612c8da2f1a372e349f&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 Zerobu

• Black Hatter

Reputation: 13
• Posts: 1,822
• Joined: 14-January 08

# Finding and listing the Highest and Lowest and lowest in a linked list

Posted 21 February 2009 - 05:33 PM

Ok, so i wanna know how do i find the lowest and highest in a linked list.

Here is my linked list implementation file.

```#include "List.h"
#include<iostream>

List::List()
{

}

{
Node *current;
Node *temp;

{

return true;

}
else
{

while(current ->next != NULL)
current = current ->next;

temp = new Node;
temp->next = NULL;
current->next = temp;

return true;

}

return false;
}

int List::ListCount()
{
count = 0;

//Assume P points to the first node in the list
for (Node * curr = head; curr != NULL; curr = curr->next)
count++;
return count;

}
int List::ListHigh()
{

for (Node * curr = head; curr != NULL; curr = curr->next)
{

high = curr ->data;

}

return 0;
}
int List::ListLow()
{

return 0;

}
ListType List::ListMean()
{
ListType total = 0;

for (Node * curr = head; curr != NULL; curr = curr->next)
total += curr->data;

mean = total / count;

return mean;
}

int List::ListRange(ListType High, ListType Low)
{

for (Node * curr = head; curr != NULL; curr = curr->next)
{
if( curr->data <= High && curr->data >= Low)
}

}

ListType List::ListSum()
{

for (Node * curr = head; curr != NULL; curr = curr->next)
sum += curr->data;

return sum;
}

List::~List()
{

}

```

Is This A Good Question/Topic? 0

## Replies To: Finding and listing the Highest and Lowest and lowest in a linked list

### #2 crazyjugglerdrummer

• GAME OVER. NERD WINS.

Reputation: 124
• Posts: 690
• Joined: 07-January 09

## Re: Finding and listing the Highest and Lowest and lowest in a linked list

Posted 21 February 2009 - 06:24 PM

What you basically need is a way to traverse the linked list from item to item. This will probably involve following the pointer in each object to the next, and then comparing values. You just need to get the values out of the list, and then find the greatest. If you need help traversing the list, look at some tutorials or post the header file.

Hope this helps!

### #3 Zerobu

• Black Hatter

Reputation: 13
• Posts: 1,822
• Joined: 14-January 08

## Re: Finding and listing the Highest and Lowest and lowest in a linked list

Posted 21 February 2009 - 07:52 PM

crazyjugglerdrummer, on 21 Feb, 2009 - 05:24 PM, said:

What you basically need is a way to traverse the linked list from item to item. This will probably involve following the pointer in each object to the next, and then comparing values. You just need to get the values out of the list, and then find the greatest. If you need help traversing the list, look at some tutorials or post the header file.

Hope this helps!

Im still lost

### #4 KYA

• Wubba lubba dub dub!

Reputation: 3202
• Posts: 19,232
• Joined: 14-September 07

## Re: Finding and listing the Highest and Lowest and lowest in a linked list

Posted 21 February 2009 - 08:06 PM

You kinda already have the framework:

```int lowest = 0, highest = 0;
lowest = highest = current->data; //assign current ptr vale to both lowest and highest
while(current->next != NULL) { //traverse the list
if (current->data < lowest)
lowest = current->data; //assign accordingly
else if (current-> > highest)
highest = current->data;
}

```

Something like that.

This post has been edited by KYA: 21 February 2009 - 08:06 PM

### #5 Zerobu

• Black Hatter

Reputation: 13
• Posts: 1,822
• Joined: 14-January 08

## Re: Finding and listing the Highest and Lowest and lowest in a linked list

Posted 21 February 2009 - 08:20 PM

KYA, on 21 Feb, 2009 - 07:06 PM, said:

You kinda already have the framework:

```int lowest = 0, highest = 0;
lowest = highest = current->data; //assign current ptr vale to both lowest and highest
while(current->next != NULL) { //traverse the list
if (current->data < lowest)
lowest = current->data; //assign accordingly
else if (current-> > highest)
highest = current->data;
}

```

Something like that.

I tried compiling that and i get alot of conversion errors. Did it work for you?

### #6 KYA

• Wubba lubba dub dub!

Reputation: 3202
• Posts: 19,232
• Joined: 14-September 07

## Re: Finding and listing the Highest and Lowest and lowest in a linked list

Posted 21 February 2009 - 08:47 PM

I didn't compile it, i was just giving the basic idea. I haven't seen your list class and thus have no idea what data actually is. I was laboring under the assumption that it was an integer.

### #7 Zerobu

• Black Hatter

Reputation: 13
• Posts: 1,822
• Joined: 14-January 08

## Re: Finding and listing the Highest and Lowest and lowest in a linked list

Posted 21 February 2009 - 09:04 PM

Here is list.h

```
typedef double ListType;

struct Node
{
ListType   data;
Node *next;

};

class List
{

public:
List();
~List();
ListType ListMean();
int ListRange(ListType High, ListType Low);
int ListCount();
int ListLow();
int ListHigh();
ListType ListSum();

private:

ListType high;
ListType sum;
ListType mean;
int count;

};

```

### #8 KYA

• Wubba lubba dub dub!

Reputation: 3202
• Posts: 19,232
• Joined: 14-September 07

## Re: Finding and listing the Highest and Lowest and lowest in a linked list

Posted 21 February 2009 - 09:11 PM

```double lowest = 0, highest = 0;
lowest = highest = current->data; //assign current ptr vale to both lowest and highest
while(current->next != NULL) { //traverse the list
if (current->data < lowest)
lowest = current->data; //assign accordingly
else if (current-> > highest)
highest = current->data;
}

```

OK so make them doubles. I'm still laboring under the assumption that your linked list works flawlessly with everything else.

### #9 Zerobu

• Black Hatter

Reputation: 13
• Posts: 1,822
• Joined: 14-January 08

## Re: Finding and listing the Highest and Lowest and lowest in a linked list

Posted 21 February 2009 - 09:23 PM

still not working maybe i should just give you everything

### #10 KYA

• Wubba lubba dub dub!

Reputation: 3202
• Posts: 19,232
• Joined: 14-September 07

## Re: Finding and listing the Highest and Lowest and lowest in a linked list

Posted 21 February 2009 - 09:24 PM

Hmm I just noticed a typo:

```double lowest = 0, highest = 0;
lowest = highest = current->data; //assign current ptr value to both lowest and highest
while(current->next != NULL) { //traverse the list
if (current->data < lowest)
lowest = current->data; //assign accordingly
else if (current->data > highest) //forgot a 'data' here
highest = current->data;
}

```

This post has been edited by KYA: 21 February 2009 - 09:24 PM

### #11 Zerobu

• Black Hatter

Reputation: 13
• Posts: 1,822
• Joined: 14-January 08

## Re: Finding and listing the Highest and Lowest and lowest in a linked list

Posted 21 February 2009 - 09:26 PM

ok here it is all

the main cpp file
```/*

/*************************************************************************
Des: This program will input student grades and allow a user to obtain the
This program enlists the help of the List class to code the solution.
Author: Coach Ragsdale
Due Date: 2/1/09 @ 11:55pm
*************************************************************************/

#include <iostream>

//contains List class definition which will be needed to provide a solution
#include "List.h"

using namespace std;

/*************************************************************************
GLOBAL DECLARATIONS
*************************************************************************/

const int EXIT = '4';

//Instantiate the List object

/*************************************************************************
Des: This function is responsible for displaying the menu to the screen
showing the user the options available for use.  The function will
also obtain the option and send it back for later use.
*************************************************************************/

{
cout << "What do you wish to do? "<< endl << endl;
cout << "ENTER 1 to obtain the mean " << endl;
cout << "ENTER 2 to obtain the mean with high and low grades dropped "
<< endl;
cout << "ENTER 3 to obtain the count of A's, B's, C's, D's and F's "
<< endl;
cout << "ENTER 4 to EXIT the program "<< endl << endl;
cout << "? ";
cin >> Opt;
}

/*************************************************************************
Function Name: PrintMean
Des: This function will print out the mean of the grades.  It will call
the method "ListMean" found in the List class.
*************************************************************************/

void PrintMean ()
{
cout.setf (ios::fixed,ios::floatfield);
cout.setf (ios::showpoint);
cout.precision(2);
cout << "The mean of the grades entered is "
<< Grades.ListMean() << endl << endl;
}

/*************************************************************************
Function Name: PrintHLmean
Des: This function will drop the high and low grades found in the List of
Grades to find an average.  It will use the methods "ListHigh",
"ListLow", "ListSum" and "ListCount" found in the List class.
*************************************************************************/

void PrintHLmean ()
{
ListType HLmean;

{
cout.setf (ios::fixed,ios::floatfield);
cout.setf (ios::showpoint);
cout.precision(2);
cout << "The mean of the grades entered with the high and low ";
cout << "grade dropped is " << HLmean << endl;
cout << endl;
}
else
cout << "You must have more than 2 grades to get this type "
<< "of mean. " << endl << endl;
}

/*************************************************************************
Des:  This function will print the number of A's, B's, C's, D's, and F's
found in the List of Grades.  Pre-defined constants are used for
the ranges found within each letter grade.  It will use the method
"ListRange" found in the List class.
*************************************************************************/

{
const ListType HIGH_A = 100;
const ListType LOW_A = 90;
const ListType HIGH_B = 89.999;
const ListType LOW_B = 80;
const ListType HIGH_C = 79.999;
const ListType LOW_C = 70;
const ListType HIGH_D = 69.999;
const ListType LOW_D = 60;
const ListType HIGH_F = 59.999;

cout << endl << endl;
cout << "A's: " << Grades.ListRange(HIGH_A,LOW_A) << endl;
cout << "B's: " << Grades.ListRange(HIGH_B,LOW_B) << endl;
cout << "C's: " << Grades.ListRange(HIGH_C,LOW_C) << endl;
cout << "D's: " << Grades.ListRange(HIGH_D,LOW_D) << endl;
cout << "F's: " << Grades.ListRange(HIGH_F,0) << endl << endl;
}

/*************************************************************************
Function Name: Execute
Des:  This function will drive the execution of the menu options.
*************************************************************************/

void Execute (char Opt)
{
switch (Opt)
{
case '1':
PrintMean();
break;

case '2':
PrintHLmean();
else
cout << "There must be more than 2 grades to get this average."
<< endl;
break;

case '3':
break;

case '4':
cout << "PROGRAM TERMINATED" << endl;
break;

default:
cout << "INVALID OPTION ... TRY AGAIN " << endl;
}
}

/*************************************************************************
Des:  This function will obtain grades from the keyboard and add them
must be terminated by a -1 to signal no more grades.
*************************************************************************/

{

cout << "Enter grade (-1 to terminate input): ";
{
}
else
{
cout << Grade << " not entered...grade list is full" << endl;
}
else
{
cout << "Enter grade (-1 to terminate input): ";
}
}

/*************************************************************************
MAIN PROGRAM
*************************************************************************/

void main()
{
char Option;

do
{
Execute (Option);
}
while (Option != EXIT);
}

```

List.h
```ypedef double ListType;

struct Node
{
ListType   data;
Node *next;

};

class List
{

public:
List();
~List();
ListType ListMean();
int ListRange(ListType High, ListType Low);
int ListCount();
int ListLow();
int ListHigh();
ListType ListSum();

private:

ListType high;
ListType sum;
ListType mean;
int count;

};

```

list.cpp
```#include "List.h"
#include<iostream>

List::List()
{

}

{
Node *current;
Node *temp;

{

return true;

}
else
{

while(current ->next != NULL)
current = current ->next;

temp = new Node;
temp->next = NULL;
current->next = temp;

return true;

}

return false;
}

int List::ListCount()
{
count = 0;

//Assume P points to the first node in the list
for (Node * curr = head; curr != NULL; curr = curr->next)
count++;
return count;

}
int List::ListHigh()
{

return 0;
}
int List::ListLow()
{

return 0;

}
ListType List::ListMean()
{
ListType total = 0;

for (Node * curr = head; curr != NULL; curr = curr->next)
total += curr->data;

mean = total / count;

return mean;
}

int List::ListRange(ListType High, ListType Low)
{

for (Node * curr = head; curr != NULL; curr = curr->next)
{
if( curr->data <= High && curr->data >= Low)
}

}

ListType List::ListSum()
{

for (Node * curr = head; curr != NULL; curr = curr->next)
sum += curr->data;

return sum;
}

List::~List()
{

}

```

This post has been edited by Steffan: 21 February 2009 - 09:27 PM

### #12 Zerobu

• Black Hatter

Reputation: 13
• Posts: 1,822
• Joined: 14-January 08

## Re: Finding and listing the Highest and Lowest and lowest in a linked list

Posted 21 February 2009 - 09:39 PM

I got the code to compile, but there is an infinite loop for some reason.

### #13 KYA

• Wubba lubba dub dub!

Reputation: 3202
• Posts: 19,232
• Joined: 14-September 07

## Re: Finding and listing the Highest and Lowest and lowest in a linked list

Posted 21 February 2009 - 09:40 PM

Do you set the last tail node's next to NULL? If there's no tail node with null it would go on forever.

### #14 Zerobu

• Black Hatter

Reputation: 13
• Posts: 1,822
• Joined: 14-January 08

## Re: Finding and listing the Highest and Lowest and lowest in a linked list

Posted 21 February 2009 - 09:43 PM

Oh i see nevermind it works now. You forgot to increment current thats why there was an infinite loop.