Kanjut SHELL
Server IP : 172.16.15.8  /  Your IP : 3.147.71.175
Web Server : Apache
System : Linux zeus.vwu.edu 4.18.0-553.27.1.el8_10.x86_64 #1 SMP Wed Nov 6 14:29:02 UTC 2024 x86_64
User : apache ( 48)
PHP Version : 7.2.24
Disable Function : NONE
MySQL : OFF  |  cURL : ON  |  WGET : ON  |  Perl : ON  |  Python : ON
Directory (0705) :  /home/bafreeman/

[  Home  ][  C0mmand  ][  Upload File  ]

Current File : /home/bafreeman/studgradelnklist.cpp
//
// File: gradelnklist.cpp.  Contents: C++ source code for dynamic memory
// allocation implementation of a list of grades.  Each node in the list 
// contains a grade and the occurrence count for that grade.
// The grades are stored in ascending order.
//

#include "gradelnklist.h"
#include <iostream>
#include <iomanip>
using namespace std;

// ---------- general list members first ----------

// --------------------------------------------------------------------
// GradeList constructor -  initializes a list to empty
// --------------------------------------------------------------------
// Accepts:  nothing
// Returns:  nothing
// --------------------------------------------------------------------

GradeList::GradeList()
{
	head = NULL;
}

// --------------------------------------------------------------------
// GradeList destructor - deletes all nodes in list
// --------------------------------------------------------------------
// Accepts:  nothing
// Returns:  nothing
// --------------------------------------------------------------------

GradeList::~GradeList()
{
	NodeType* tmp;    // temp ptr for deleting nodes

	while (head != NULL)    // destroy nodes one by one
	{
	    tmp = head;
	    head = head->next;
	    delete tmp;
	}
}

// --------------------------------------------------------------------
// IsEmpty - test for empty list
// --------------------------------------------------------------------
// Accepts:  nothing
// Returns:  true if the list is empty, false otherwise.
// --------------------------------------------------------------------

bool GradeList::IsEmpty(void)
{
	return (head == NULL);
}

// --------------------------------------------------------------------
// IsFull - test for full list
// --------------------------------------------------------------------
// Accepts:  nothing
// Returns:  false always, because, within the bounds of memory size,
//           the list never fills!
// --------------------------------------------------------------------

bool GradeList::IsFull()
{
	return false;
}

// -------------------------------------------------------------------
// Display - prints the grades in the list, one grade per line
// -------------------------------------------------------------------
// Accepts:  nothing
// Returns:  nothing
// -------------------------------------------------------------------

void GradeList::Display()
{
	NodeType* tmp = head;   // temp ptr for traversing list

	if (IsEmpty())
	    cout << "\nThere are no grades\n\n";
	else
	{
	    cout << "\nGrades    Num of Each \n\n";
	    while (tmp != NULL)
	    {
	        cout << setw(4) << tmp->grade << setw(14) << tmp->count << "\n";
	        tmp = tmp->next;
	    }
	}

}

// -------------------------------------------------------------------
// Function to insert a new grade into the list in ascending order
// -------------------------------------------------------------------
// Accepts:  the new grade
// Returns:  nothing
// -------------------------------------------------------------------

void GradeList::InsertGrade(int newgrade)
{
	NodeType* curr = head;   // temp ptr for traversing list
	NodeType* prev = NULL;   // temp ptr for traversing list
	NodeType* tmp;           // temp ptr to create new node

	while (curr != NULL && curr->grade < newgrade)  // find place to insert
	{
	    prev = curr;
	    curr = curr->next;
	}

	if (curr != NULL && curr->grade == newgrade)    // grade already here ...
	    curr->count++;                              // ... simply inc. count
	else
	    if (prev == NULL)    // empty list or insert at beginning
	    {
	        tmp = new NodeType;
	        tmp->grade = newgrade;
		tmp->count = 1;
		tmp->next = head;
		head = tmp;
	    }
	    else                 // insert in middle or at end
	    {
		tmp = new NodeType;
		tmp->grade = newgrade;
		tmp->count = 1;
		tmp->next = curr;
	        prev->next = tmp;
	    }
}

// -------------------------------------------------------------------
// Function to delete the indicated grade from the list
// -------------------------------------------------------------------
// Accepts:  grade to delete
// Returns:  nothing
// -------------------------------------------------------------------

void GradeList::DeleteGrade(int delgrade)
{
	NodeType* curr = head;   
        NodeType* prev = NULL;   
        NodeType* tmp;           

   	if(!IsEmpty())
	{         
        	while (curr != NULL && curr->grade < delgrade) 
        	{
        	    prev = curr;
        	    curr = curr->next; 
        	}
      
		if (curr !=NULL && curr->grade == delgrade)
			curr->count--;
		else
			if(curr->count==1)
			{	
				if(prev==NULL)
				{
					tmp= head;
					tmp= tmp->next;
					delete head;
					head= tmp;
				}
				else
				{
					tmp=curr;
					tmp= tmp->next;
					delete curr;
					curr= tmp;
				}
			}
	}
	else
		cout << "There are no Grades.\n";
}

// -------------------------------------------------------------------
// Function to calculate the average of the grades in the list
// -------------------------------------------------------------------
// Accepts:  nothing
// Returns:  the average (0 if list is empty)
// -------------------------------------------------------------------

float GradeList::CalculateAvg()
{
	NodeType* curr =head;
	float gradesum=0;  // sum of grades
	float countsum=0; //  total number of grades
	float avg;  // Average
	
	if(!IsEmpty())  // The list is not empty
	{
		while(curr !=NULL)  // tally up the number of grades and their sum
		{
			gradesum= gradesum+curr->grade; 
			countsum= countsum+curr->count;
			curr=curr->next;
		}
		
		avg = gradesum/countsum;  // represents the average
		return avg;
	}
	else
		return 0;	

}

// -------------------------------------------------------------------
// DropLowestGrade - drops the lowest grade in the list, does nothing
//                   if list is empty
// -------------------------------------------------------------------
// Accepts:  nothing
// Returns:  nothing
// -------------------------------------------------------------------

void GradeList::DropLowestGrade()
{
	NodeType * tmp = head;  // temp ptr for transversing through the list

	if(!IsEmpty())
	{
		if(tmp->count == 1)  // There is only one 
                {                    // occurance of the grade	
                	tmp= head;  
                	tmp= tmp->next;
                	delete head;
                	head= tmp;
                }
		else  // There's more than one
			tmp->count--;
	}
	else
		cout << "\nThere are no grades\n";

}
     
// -------------------------------------------------------------------
// HowManyofGrade -  returns how many times this grade is in the list
// -------------------------------------------------------------------
// Accepts:  the grade to look for
// Returns:  the number of times the grade is in the list
// -------------------------------------------------------------------

int GradeList::HowManyofGrade (int grade)
{
	NodeType* curr = head;   // temp ptr for traversing list
	NodeType* prev = NULL;   // temp ptr for traversing list

	int a=0;  // number of grades

	if(!IsEmpty())
	{
		while (curr != NULL && curr->grade < grade)  // search for grade in question
  		{
            		prev = curr;
            		curr = curr->next;
        	}
		
		if(curr != NULL && curr->grade == grade) // find how many times the grade appears
		{
			a= curr->count;
		}

		return a;
	}
	else
		return 0;

}


Stv3n404 - 2023