So im having issues with a sorted list template, heres how I implemented it
enum RelationType {LESS, EQUAL, GREATER};
template<class ItemType>
struct NodeType
{
ItemType info;
NodeType<ItemType>* next;
};
template<class ItemType>
class SortedType
{
public:
SortedType();
~SortedType();
bool IsFull() const;
int GetLength() const;
void MakeEmpty();
ItemType GetItem(ItemType item, bool& found);
void PutItem(ItemType item);
void DeleteItem(ItemType item);
void ResetList();
ItemType GetNextItem();
private:
NodeType<ItemType>* listData;
int length;
NodeType<ItemType>* currentPos;
};
template<class ItemType>
SortedType<ItemType>::SortedType()
{
length = 0;
listData = NULL;
}
template<class ItemType>
bool SortedType<ItemType>::IsFull() const
{/*
NodeType<ItemType>* location;
try
{
location = new NodeType<ItemType>;
delete location;
return false;
}
catch(bad_alloc exception)
{
return true;
}*/
return false;
}
template<class ItemType>
int SortedType<ItemType>::GetLength() const
{
return length;
}
template<class ItemType>
void SortedType<ItemType>::MakeEmpty()
{
NodeType<ItemType>* tempPtr;
while(listData != NULL)
{
tempPtr = listData;
listData = listData->next;
delete tempPtr;
}
length = 0;
}
template<class ItemType>
ItemType SortedType<ItemType>::GetItem(ItemType item, bool& found)
{
bool moreToSearch;
NodeType<ItemType>* location;
location = listData;
found = false;
moreToSearch = (location != NULL);
while(moreToSearch && !found)
{
switch(item.ComparedTo(location->next->info))
{
case GREATER: location = location->next;
moreToSearch = (location != NULL);
break;
case EQUAL: found = true;
item = location->info;
break;
case LESS: moreToSearch = false;
break;
}
}
return item;
}
template<class ItemType>
void SortedType<ItemType>::PutItem(ItemType item)
{
cout<<"big";
NodeType<ItemType>* newNode;
NodeType<ItemType>* predLoc;
NodeType<ItemType>* location;
bool moreToSearch;
location = listData;
predLoc = NULL;
moreToSearch = (location != NULL);
while(moreToSearch)
{
switch(item.ComparedTo(location->info))
{
case GREATER:
predLoc = location;
location = location->next;
moreToSearch = (location != NULL);
break;
case LESS: moreToSearch = false;
break;
case EQUAL:moreToSearch = false;
break;
}
}
newNode = new NodeType<ItemType>;
newNode->info = item;
if(predLoc == NULL)
{
newNode->next = listData;
listData = newNode;
}
else
{
newNode->next = location;
predLoc->next = newNode;
}
length++;
}
template<class ItemType>
void SortedType<ItemType>::DeleteItem(ItemType item)
{
NodeType<ItemType>* location = listData;
NodeType<ItemType>* tempLocation;
if(item.ComparedTo(listData->info) == EQUAL)
{
tempLocation = location;
listData = listData->next;
}
else
{
while((item.ComparedTo(location->next->info) != EQUAL))
{
location = location->next;
tempLocation = location->next;
length--;
}
}
}
template<class ItemType>
void SortedType<ItemType>::ResetList()
{
currentPos = NULL;
}
template<class ItemType>
ItemType SortedType<ItemType>::GetNextItem()
{
ItemType item;
if(currentPos == NULL)
currentPos = listData;
item = currentPos->info;
currentPos = currentPos->next;
return item;
}
template<class ItemType>
SortedType<ItemType>::~SortedType()
{
NodeType<ItemType>* tempPtr;
while(listData != NULL)
{
tempPtr = listData;
listData = listData->next;
delete tempPtr;
}
}
now the problem is when I set up the struct to be sorted
struct waitList
{
string HoliName;
QueueType<string> CustName;
RelationType ComparedTo(waitList lists)
{
cout<<endl<<HoliName.compare(lists.HoliName)<<endl<<endl;
if(HoliName.compare(lists.HoliName)<0)
return LESS;
if(HoliName.compare(lists.HoliName)>0)
return GREATER;
cout<< "no";
if(HoliName.compare(lists.HoliName)==0)
{
cout<<"yay";
return EQUAL;
cout<<"woo";
}
else
return LESS;
}
};
and then insert the second item with
void populateWait(SortedType<waitList>& waiting, UnsortedType<string> holidays)
{
waitList dummy;
string blank;
int j;
holidays.ResetList();
waiting.ResetList();
while(!holidays.IsLastItem())
{
waiting.ResetList();
holidays.GetNextItem(blank);
dummy.HoliName=blank;
dummy.CustName.MakeEmpty();
cout<<blank;
waiting.PutItem(dummy);
cin>>j;
}
}
It crashes, but strangley enough it crashes right at the
if(HoliName.compare(lists.HoliName)==0)
return EQUAL;
Literally right at the return statemet(I wrote a cout right at the calling function and got nothing), I know this template isnt faulty as sorting based on an integer in a struct works fine... is there anything special I need to do with strings, or a special bit of code im missing?