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

Page 1 of 1

## 13 Replies - 899 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=06db9a540cfc0ad9110d8a2efd086759&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()
{
head = NULL;

}

bool List::  ListAdd(ListType Grade)
{
Node *current;
Node *temp;

if(head == NULL)
{
head = new Node;
head ->data = Grade;	//Puts the data in the new node
head->next = NULL;

return true;

}
else
{
current = head;

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

temp = new Node;
temp->data = Grade;
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)
{

if(curr->data > head->data)
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)
{
grade_count = 0;

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

return grade_count;

}

ListType List::ListSum()
{

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

return sum;
}

List::~List()
{

delete head;

}

```

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: 120
• 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!
Was This Post Helpful? 0

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

### #4 KYA

• yay verily

Reputation: 3155
• Posts: 19,198
• 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

Was This Post Helpful? 0

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

### #6 KYA

• yay verily

Reputation: 3155
• Posts: 19,198
• 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.
Was This Post Helpful? 0

### #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();
bool ListAdd(ListType Grade);
ListType ListSum();

private:

Node *head;
ListType high;
ListType sum;
int grade_count;
ListType mean;
int count;

};

```

Was This Post Helpful? 0

### #8 KYA

• yay verily

Reputation: 3155
• Posts: 19,198
• 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.
Was This Post Helpful? 0

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

### #10 KYA

• yay verily

Reputation: 3155
• Posts: 19,198
• 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

Was This Post Helpful? 0

### #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
```/*

/*************************************************************************
Program Name: GradeAna.cpp
Des: This program will input student grades and allow a user to obtain the
mean of the grades, mean with high and low grades dropped and grade ranges.
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
List Grades;

/*************************************************************************
Function Name: MenuPrompt
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.
*************************************************************************/

void MenuPrompt (char &Opt)
{
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;

if (Grades.ListCount() > 2)
{
HLmean = (Grades.ListSum() - (Grades.ListHigh() + Grades.ListLow()))
/ (Grades.ListCount() - 2);
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;
}

/*************************************************************************
Function Name: PrintGradeCount
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.
*************************************************************************/

void PrintGradeCount ()
{
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.
Parameters: opt - contains the menu option obtained in MenuPrompt.
*************************************************************************/

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

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

case '3':
PrintGradeCount();
break;

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

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

/*************************************************************************
Function Name: GetGrades
Des:  This function will obtain grades from the keyboard and add them
to the Grades object by using the method ListAdd.  Grades entered
must be terminated by a -1 to signal no more grades.
*************************************************************************/

void GetGrades ()
{
const int LOWGRADE = 0;
const int HIGHGRADE = 100;
ListType Grade;

cout << "Enter grade (-1 to terminate input): ";
cin >> Grade;
while (Grade != -1)
if (Grade < LOWGRADE || Grade > HIGHGRADE)
{
cout << "Grade out of range...re-enter grade please: ";
cin >> Grade;
}
else
if (!Grades.ListAdd(Grade))
{
cout << Grade << " not entered...grade list is full" << endl;
Grade = -1;
}
else
{
cout << "Enter grade (-1 to terminate input): ";
cin >> Grade;
}
}

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

void main()
{
char Option;

GetGrades();
if (Grades.ListCount() > 0)
do
{
MenuPrompt (Option);
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();
bool ListAdd(ListType Grade);
ListType ListSum();

private:

Node *head;
ListType high;
ListType sum;
int grade_count;
ListType mean;
int count;

};

```

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

List::List()
{
head = NULL;

}

bool List::  ListAdd(ListType Grade)
{
Node *current;
Node *temp;

if(head == NULL)
{
head = new Node;
head ->data = Grade;	//Puts the data in the new node
head->next = NULL;

return true;

}
else
{
current = head;

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

temp = new Node;
temp->data = Grade;
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)
{
grade_count = 0;

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

return grade_count;

}

ListType List::ListSum()
{

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

return sum;
}

List::~List()
{

delete head;

}

```

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

Was This Post Helpful? 0

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

### #13 KYA

• yay verily

Reputation: 3155
• Posts: 19,198
• 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.
Was This Post Helpful? 0

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