Need a Walkthrough of a Program using a Linked List

I need help implenting the functionality of this application

Page 1 of 1

12 Replies - 2478 Views - Last Post: 21 February 2009 - 04:17 PM Rate Topic: -----

#1 Zerobu   User is offline

  • Black Hatter

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

Need a Walkthrough of a Program using a Linked List

Post icon  Posted 15 February 2009 - 07:25 PM

Ok so i need help writing code that uses a link list, here is the assignment


Computer Science 245 - Assignment 2
Due Date: Feb 22, 2008 (11:55 PM)
Points: 50
A Teacher EnLISTs Your Help
Programming Objectives
1) Defining and Implementing the List ADT as a linked list.
2) Understand how an ADT can be used to solve a problem.
3) Observe how objects are used in an application.
4) Creating a program using multiple source files
Assignment Description:
This assignment calls for you to define and implement a List ADT which can be used in a grade analysis application. This application is already provided for you. This program file is called GradeAna.cpp and can be found on the class website (documentation example).
The List ADT for this assignment will be implemented using a linked list. Creating and manipulating pointer variables will be a must because memory will be created “on the fly”. This is what is referred to as dynamic allocation of memory. We are implementing a list as a linked list to concentrate on memory efficiency not access efficiency.
The List class will have the following functionality:
1) Constructor
2) Destructor
3) Compute and return the mean for the List
4) Add a ListType to the List and return success or failure of this operation
5) Find and return the highest ListType in the List
6) Find and return the lowest ListType in the List
7) Compute and return the sum of the ListTypes in the List
8) Return the count of ListTypes in the List
9) Calculate and return a count of ListTypes within a user defined numeric range
The definition for the List class along with other declarations and definitions must be contained in a file called List.h. You will need to typedef a double as a ListType. You will also need to define a Node in a linked list.
The implementation for the List class must be contained in a file called List.cpp. This file must be compiled using List.h and then linked with GradeAna.cpp to create the application program.
Please turn in both source files (list.h , list.cpp) using EASEL.


Here is the cpp main 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);
}



Is This A Good Question/Topic? 0
  • +

Replies To: Need a Walkthrough of a Program using a Linked List

#2 BlakeJustBlake   User is offline

  • D.I.C Regular
  • member icon

Reputation: 26
  • View blog
  • Posts: 441
  • Joined: 15-February 09

Re: Need a Walkthrough of a Program using a Linked List

Posted 15 February 2009 - 08:11 PM

Looks like you need to make a Linked List class and add some functionality to it like finding the mean of the values and such. You should have learned this in class. If you understand how to make a linked list, then you should also understand how to traverse it, and if you understand how to traverse it then you should also understand how to get the data from the Nodes. If you can traverse it and get data from the Nodes in a way that fulfills all the functions, then you'll be fine. If you can't do this then I suggest going back and reading your notes or book.
Was This Post Helpful? 0
  • +
  • -

#3 Zerobu   User is offline

  • Black Hatter

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

Re: Need a Walkthrough of a Program using a Linked List

Posted 15 February 2009 - 08:52 PM

lol i don't think i understand any of that stuff, luckily i have a week before this program is due.
Was This Post Helpful? 0
  • +
  • -

#4 BlakeJustBlake   User is offline

  • D.I.C Regular
  • member icon

Reputation: 26
  • View blog
  • Posts: 441
  • Joined: 15-February 09

Re: Need a Walkthrough of a Program using a Linked List

Posted 15 February 2009 - 09:16 PM

Try Starting Here

If you have any questions about Linked Lists in general, such as how what works or something. I'd be happy to shed some light, or if you'd like me to give you my quick and dirty explanation of them.
Was This Post Helpful? 0
  • +
  • -

#5 JackOfAllTrades   User is offline

  • Saucy!
  • member icon

Reputation: 6259
  • View blog
  • Posts: 24,028
  • Joined: 23-August 08

Re: Need a Walkthrough of a Program using a Linked List

Posted 15 February 2009 - 09:30 PM

Data Structures in C++ Tutorial

And another Linked List Tutorial
Was This Post Helpful? 0
  • +
  • -

#6 Zerobu   User is offline

  • Black Hatter

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

Re: Need a Walkthrough of a Program using a Linked List

Posted 16 February 2009 - 04:42 PM

Ok i got everything to compile but know i need to start on the implementation.

Here is my .h file and my implementation file.

typedef double ListType;


struct Node
{
		int count;
		Node *next;
	
};


class List
{

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



private:

	Node *head;
	int count;


	
};





#include "List.h"


List::List()
{


}

int List::  ListAdd(ListType Grade)
{



	return 0;
}

int List::ListCount()
{




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


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


	return 0;


}
int List::ListMean()
{



	return 0;
}

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



	return 0;


}

int List::ListSum()
{



	return 0;
}

List::~List()
{


	

}



Was This Post Helpful? 0
  • +
  • -

#7 crazyjugglerdrummer   User is offline

  • GAME OVER. NERD WINS.
  • member icon

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

Re: Need a Walkthrough of a Program using a Linked List

Posted 16 February 2009 - 04:49 PM

Now think about what each function will do and how to accomplish that. The constructor might involve making a head object for the list where the add function will connect the pointer in the last element of the list to the new element. Then you need ways to access and link through the data, which is just following the pointers x number of times. From there, any other operations you want to perform on the list work as if it were another data structure, just using the other methods you made.

Happy coding!
Was This Post Helpful? 0
  • +
  • -

#8 Zerobu   User is offline

  • Black Hatter

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

Re: Need a Walkthrough of a Program using a Linked List

Posted 16 February 2009 - 05:37 PM

I was just wondering, should a single Linked List Always have a head, tail, and next objects?
Was This Post Helpful? 0
  • +
  • -

#9 Zerobu   User is offline

  • Black Hatter

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

Re: Need a Walkthrough of a Program using a Linked List

Posted 16 February 2009 - 07:12 PM

I 'am so fucking confused..

This post has been edited by Steffan: 17 February 2009 - 09:49 AM

Was This Post Helpful? 0
  • +
  • -

#10 Zerobu   User is offline

  • Black Hatter

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

Re: Need a Walkthrough of a Program using a Linked List

Posted 17 February 2009 - 06:51 PM

Ok so i have gotten two of my functions done but i don't know how to traverse the list to access all the element.

here is my code.

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


List::List()
{
	head = NULL;

}

int List::  ListAdd(ListType Grade)
{


  
	
  Node *q,*t;

   if( head == NULL )
   {
		head = new Node;
	  head->data = Grade;  //Add Grade to List
	  count++;
	  head-> next = NULL;

	  return 1;
   }
   else
   {
		q = head;
	  while( q->next != NULL )
		   q = q->next;


	  t = new Node;
	  t->data = Grade;   //Add Grade to list
	  count++;
	  t->next = NULL;
	  q->next = t;

	  return 1;
   }

   return 0;


}

int List::ListCount()
{


	return count;
}
int List::ListHigh()
{


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


	return 0;


}
int List::ListMean()
{



	return 0;
}

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



	return 0;


}

int List::ListSum()
{



	return 0;
}

List::~List()
{


	

}



List.h
typedef double ListType;


struct Node
{
		int data;

		Node *next;
	
};


class List
{

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



private:

	Node *head;
	int count;


	
};



GradeAna.cpp
/*

/*************************************************************************
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);
}



Was This Post Helpful? 0
  • +
  • -

#11 Zerobu   User is offline

  • Black Hatter

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

Re: Need a Walkthrough of a Program using a Linked List

Posted 17 February 2009 - 07:37 PM

I need to traverse the list

This post has been edited by Steffan: 17 February 2009 - 07:37 PM

Was This Post Helpful? 0
  • +
  • -

#12 Zerobu   User is offline

  • Black Hatter

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

Re: Need a Walkthrough of a Program using a Linked List

Posted 21 February 2009 - 02:21 PM

bump
Was This Post Helpful? 0
  • +
  • -

#13 Sadaiy   User is offline

  • D.I.C Head

Reputation: 3
  • View blog
  • Posts: 107
  • Joined: 03-October 08

Re: Need a Walkthrough of a Program using a Linked List

Posted 21 February 2009 - 04:17 PM

this part right here :

q = head;
	  while( q->next != NULL )
		   q = q->next;



is pretty much how you traverse list.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1