Ok, I already have my SortedLinkedList.h and my runSortedLinkedList.cxx up, so here is the rest of the code:
Code:
SortedLinkedList.cxx
#include "SortedLinkedList.h"
template<class ItemType>
SortedType<ItemType>::SortedType() // Class constructor
{
length = 0;
listData = NULL;
}
template<class ItemType>
SortedType<ItemType>::~SortedType()
// Post: List is empty; all items have been deallocated.
{
MakeEmpty();
}
template<class ItemType>
SortedType<ItemType>::SortedType(const SortedType& otherList)
// Copy-constructor
// Postcondition:
// IF otherList.listData == NULL
// listData == NULL
// ELSE
// listData points to a new linked list that is a copy of
// the linked list pointed to by otherList.listData
{
NodeType<ItemType>* fromPtr; // Pointer into list being copied from
NodeType<ItemType>* toPtr; // Pointer into new list being built
if(otherList.listData == NULL)
{
listData = NULL;
return;
}
// Copy first node
fromPtr = otherList.listData;
listData = new NodeType<ItemType>;
listData->info = fromPtr->info;
// Copy remaining nodes
toPtr = listData;
fromPtr = fromPtr->next;
while (fromPtr != NULL)
{
toPtr->next = new NodeType<ItemType>;
toPtr = toPtr->next;
toPtr->info = fromPtr->info;
fromPtr = fromPtr->next;
}
toPtr->next = NULL;
}
/* bool SortedType::IsFull() const
// Returns true if there is no room for another ItemType
// on the free store; false otherwise.
{
NodeType* location;
try
{
location = new NodeType;
delete location;
return false;
}
catch(bad_alloc exception)
{
return true;
}
} */
template<class ItemType>
bool SortedType<ItemType>::IsFull() const
// Returns true if there is no room for another ItemType
// object on the free store; false otherwise.
{
NodeType<ItemType>* ptr;
ptr = new NodeType<ItemType>;
if (ptr == NULL)
return true;
else
{
delete ptr;
return false;
}
}
template<class ItemType>
int SortedType<ItemType>::LengthIs() const
// Post: Number of items in the list is returned.
{
return length;
}
template<class ItemType>
void SortedType<ItemType>::MakeEmpty()
// Post: List is empty; all items have been deallocated.
{
NodeType<ItemType>*
tempPtr;
while (listData != NULL) // traverse list, deallocating each node in turn
{
tempPtr = listData;
listData = listData->next;
delete tempPtr;
}
length = 0; // to agree with the fact that all nodes are deallocated
}
template<class ItemType>
void SortedType<ItemType>::DeleteItem(ItemType item)
// Pre: item's key has been initialized.
// An element in the list has a key that matches item's.
// Post: No element in the list has a key that matches item's.
{
NodeType<ItemType>* location = listData;
NodeType<ItemType>* tempLocation;
// Locate node to be deleted.
if (item == listData->info)
{
tempLocation = location;
listData = listData->next; // Delete first node.
}
else
{
while (!(item==(location->next)->info))
location = location->next;
// Delete node at location->next
tempLocation = location->next;
location->next = (location->next)->next;
}
delete tempLocation;
length--;
}
template<class ItemType>
void SortedType<ItemType>::ResetList()
// Post: Current position has been initialized to AtEnd
{
currentPos = NULL;
}
template<class ItemType>
void SortedType<ItemType>::RetrieveItem(ItemType& item, bool& found)
{
bool moreToSearch;
NodeType<ItemType>* location;
location = listData;
found = false;
moreToSearch = (location != NULL);
while (moreToSearch && !found)
{
if (location->info < item)
{
location = location->next;
moreToSearch = (location != NULL);
}
else if (item == location->info)
{
found = true;
item = location->info;
}
else
moreToSearch = false;
}
}
template<class ItemType>
void SortedType<ItemType>::InsertItem(ItemType item)
{
NodeType<ItemType>* newNode; // pointer to node being inserted
NodeType<ItemType>* predLoc; // trailing pointer
NodeType<ItemType>* location; // traveling pointer
bool moreToSearch;
location = listData;
predLoc = NULL;
moreToSearch = (location != NULL);
// Find insertion point.
while (moreToSearch)
{
if (location->info < item)
{
predLoc = location;
location = location->next;
moreToSearch = (location != NULL);
}
else
moreToSearch = false;
}
// Prepare node for insertion
newNode = new NodeType<ItemType>;
newNode->info = item;
// Insert node into list.
if (predLoc == NULL) // Insert as first
{
newNode->next = listData;
listData = newNode;
}
else
{
newNode->next = location;
predLoc->next = newNode;
}
length++;
}
template<class ItemType>
void SortedType<ItemType>::GetNextItem(ItemType& item)
// Pre: Current position is defined.
// Element at current position is not last in list.
// Post: Current position has been updated; item is current item.
{
if (currentPos == NULL) //Wrap at end of list
currentPos = listData;
item = currentPos->info;
currentPos = currentPos->next;
}
template<class ItemType>
void SortedType<ItemType>::Print()
{
currentPos = listData;
while(currentPos != NULL)
{
cout << currentPos->info << " ";
currentPos = currentPos->next;
}
}
ItemType.h
#include <fstream.h>
#include <iostream.h>
#include <iomanip.h>
const int MAX_ITEMS = 10;
enum RelationType {LESS, EQUAL, GREATER};
class ItemType
{
public :
ItemType();
// PURPOSE: To set each variable with a default value
// INPUT: none
// PRE: all variables exist
// OUTPUT: none
// POST: default data is assigned
// NOTE: none
ItemType(int id, float exam1, float exam2, float final, float prog,
float quizzes, char addOrDelete);
// PURPOSE: This is used to initialize values to variables
// INPUT: id, exam1, exam2, final, prog, quizzes, addOrDelete
// PRE: all variables are assinged and ok
// OUTPUT: none
// POST: id, exam1, exam2, final, prog, quizzes, and addOrDelete are
// assinged
// NOTE: none
RelationType ComparedTo(ItemType) const;
// PURPOSE: This comapres one item of ItemType to another item to return
// either greater, less, or equal
// INPUT: ItemType
// PRE: ItemType is valid and assinged
// OUTPUT: LESS, GREATER, or EQUAL
// POST: That less, greater, or equal is returned
// NOTE: none
char GetItemFromFile(ifstream& inFile);
// PURPOSE: This will read the items from an inFile, and will also
// determine what variables to read by it's first character
// INPUT: inFile
// PRE: inFile is opened and OK
// OUTPUT: addOrDelete
// POST: all variables are assinged and addOrDelete is returned
// NOTE: none
void WriteItemToFile(ofstream& outFile) const;
// PURPOSE: This will write all variables (id, exam1, exam2, final, prog,
// and quizzes, to an outFile
// INPUT: none
// PRE: outFile is opened and OK
// OUTPUT: outFile
// POST: all variables are assigned to the outFile
// NOTE: none
void PrintLstFullErr(ofstream& outFile) const;
// PURPOSE: This will print the error message when the list is full and
// is trying to be added too
// INPUT: none
// PRE: list is full and trying to be written to
// OUTPUT: outFile
// POST: error message is written to outFile
// NOTE: none
void PrintEmptyLstErr(ofstream& outFile);
// PURPOSE: This will print an error message when the list is empty but
// a line is trying to be deleted from it
// INPUT: none
// PRE: list is empty and is trying to be deleted from
// OUTPUT: outFile
// POST: error message is written to the outFile
// NOTE: none
private :
int id;
float exam1;
float exam2;
float final;
float prog;
float quizzes;
char addOrDelete;
} ;
I understand perfectly what you are saying. My only problem is, I don't know how to fix it. I cannot put an include "ItemType.h" in my SortedLinkedList.cxx file, because I already have it in my SortedLinkedList.h file. I don't get why it wouldn't be seeing the definition of ItemType, because I have ItemType.h included in my SoretedLinkList.h file.