All pastes #800215 Raw Edit

Unnamed

public text v1 · immutable
#800215 ·published 2007-11-28 06:40 UTC
rendered paste body

#include <iostream>
#include <fstream>
#include <string>
#include <iomanip>
#include "guestLinkedListClass.h"

using namespace std;




//************************************************************
// DinnerGuestList()- Constructor for class DinnerGuestList
// Sets head to null and assigns 0 to every element in numOfMeals array
// Returns: void
//*************************************************************
DinnerGuestList::DinnerGuestList()
{
	head = NULL; // point head to NULL
	
	for (int i = 0; i < MAX_MEAL_TYPES; i++)
	{
		numOfMeals[i] = 0;
	}
	
} // DinnerGuestList




//************************************************************
// DinnerGuestList()- Destructor for class DinnerGuestList
// calls FreeMem to delete linked list
// Returns: void
//*************************************************************
DinnerGuestList::~DinnerGuestList()
{
	
	FreeMem();
	
} // DinnerGuestList




//************************************************************
// InsertSorted()- Takes name and checks the linked list
// if the name is found, it deletes it from the linked list
// Returns: void
//*************************************************************
void DinnerGuestList::InsertSorted(char name[], // char array for name
								   MealType meal) // MealType for meal
{
	
	DinnerGuest *    currPtr = NULL; // DinnerGuest pointer
	DinnerGuest *    prevPtr = NULL; // DinnerGuest pointer
	DinnerGuest *    newNodePtr = NULL; //DinnerGuest pointer

	//allocate memory for new NodeType
    newNodePtr = new  DinnerGuest;
    
	strcpy(newNodePtr->guestName, name); // copies name into linked list
	
	newNodePtr->mealTyp = meal; // copies meal into linked list
	
	numOfMeals[meal]++; // updates numOfMeals

	currPtr = head;
	
	while ( currPtr != NULL  &&  strcmp( name, currPtr->guestName ) != 0 ) 
	{	
		prevPtr = currPtr ; // advance both pointers 
		currPtr = currPtr->next ;
	}
	
	newNodePtr->next = currPtr ;	   // insert new node here

	if  ( prevPtr == NULL ) 
   		head = newNodePtr ;
	else
		prevPtr->next = newNodePtr ;
	
} // InsertSorted




//************************************************************
// IsPresent()- Takes name and checks the linked list
// if the name is found it returns true
// Returns: bool
//*************************************************************
bool DinnerGuestList::IsPresent ( const char * const guestName) // name to look for
{
	
	DinnerGuest * tempPtr = head; // DinnerGuest pointer
	
	while (tempPtr != NULL) // while tempPtr isnt NULL
	{
		
		if(strcmp(tempPtr->guestName, guestName) == 0) // compares names
		{
			return true;
		}
		tempPtr = tempPtr->next;
		
	}
	
	return false;
	
} // IsPresent




//************************************************************
// DeleteGuest()- Takes name and checks the linked list
// if the name is found, it deletes it from the linked list
// Returns: void
//*************************************************************
void DinnerGuestList::DeleteGuest ( const char * const guestName) // name to delete
{
	
	DinnerGuest *   delPtr = NULL; // DinnerGuest pointer
	DinnerGuest *   currPtr = NULL; // DinnerGuest pointer
	
	
	//check to see if guestName is in first node
	if ( strcmp( guestName, head->guestName) == 0 )
	{	
		delPtr = head ;		// If so, delete first node	
		head = head->next ; //link is new head of list
	} 				
	else 
	{	// search for guestName in rest of list
		currPtr = head ;
		while ( strcmp( currPtr->next->guestName, guestName) != 0 )
			currPtr = currPtr->next ;
		delPtr = currPtr->next ;
		currPtr->next = currPtr->next->next ;
	}
	
	numOfMeals[delPtr->mealTyp]--;
	
	delete  delPtr ;//delete node
	
	
} // DeleteGuest




//************************************************************
// PrintGuestList()- Takes output file stream and prints
// the linked list data
// Returns: void
//*************************************************************
void DinnerGuestList::PrintGuestList(fstream & outFile) // output file stream
{
	
	DinnerGuest * tempHead = head; // DinnerGuest pointer
	
	outFile << setw(WIDTH1) << "Guest Name" << setw(WIDTH2) << "   Meal Type\n";
	PrintChar(outFile, '-', WIDTH1+WIDTH2+WIDTH2); // calls PrintChar

	while (tempHead != NULL) // while tempHead isnt NULL
	{

		outFile << setw(WIDTH1);
		outFile << tempHead->guestName;
		outFile << setw(WIDTH2);
		outFile << MEAL_NAMES[tempHead->mealTyp];
		outFile << "\n";
		
		tempHead = tempHead->next; // updates tempHead to next node
		
	}
	
	PrintChar(outFile, '-', WIDTH1+WIDTH2+WIDTH2);
	
	outFile << "\n";
	
} //PrintGuestList




//************************************************************
// PrintChar()- Takes two inputs, char and int, and prints a
// a specific amount of characters based on the int 
// Returns: NA
//*************************************************************
void DinnerGuestList::PrintChar(fstream & outFile, //output filestream to print char
               char ch, //character to print
               int howMany)  //number of times to print character
{

    for(int i = 0; i < howMany; i++)
    {
        outFile << ch; // prints character
    }

    outFile << "\n"; // new line

} //PrintChar




//************************************************************
// PrintMeals()- Takes output file and
// prints out how many of each type of meals were stored
// Returns: NA
//*************************************************************
void DinnerGuestList::PrintMeals(fstream & outFile) // number of meals array
{
	
	for (int i = 0; i < MAX_MEAL_TYPES; i++)
	{
		outFile << "The number of people eating " << MEAL_NAMES[i] << " is " << numOfMeals[i] << "\n";
	}

	outFile << "\n"; // new line
	
} // PrintMeals




//************************************************************
// UpdateGuest()- Takes name and meal and checks the linkedlist
// for the name then updates the mealtype for that name
// Returns: bool
//*************************************************************
bool DinnerGuestList::UpdateGuest(char name[], // char array for name 
								  MealType meal) // MealType for meal
{
	DinnerGuest * tempHead = head; // DinnerGuest pointer
	
	while ( tempHead != NULL) // while head isnt NULL
	{
		
		if ( strcmp(tempHead->guestName, name) == 0) //compares names
		{
			
			numOfMeals[tempHead->mealTyp]--;
			numOfMeals[meal]++;
			tempHead->mealTyp = meal; // updates meal type
			
			return true;
			
		}
		
		tempHead = tempHead->next; // update to next node
	} // while
	
	return false;
} // UpdateGuest




//************************************************************
// FreeMem()- Transverses through the linked list deleting each node
// Returns: NA
//*************************************************************
void DinnerGuestList::FreeMem()
{
	
	while (head != NULL)
	{
		DinnerGuest * tempPtr = head;
		head = head->next;
		delete tempPtr; // delete node
		tempPtr = NULL;
	}
	
} // FreeMem