#include "Sorted.h" #ifndef _SORTED_LINKED_CPP_ #define _SORTED_LINKED_CPP_ template SortedType::SortedType() { listData = NULL; currentPos = NULL; } template void SortedType::MakeEmpty() { NodeType* tempPtr; while(listData)// != NULL) { tempPtr = listData; listData = listData->next; delete tempPtr; } } template bool SortedType::IsFull ( ) const { NodeType* location; try { location = new NodeType; delete location; return false; } catch(bad_alloc exception) { return true; } } template int SortedType::LengthIs ( ) const { NodeType* location = listData; int count = 0; while (location) { count++; location = location->next; } return count; } template void SortedType::RetrieveItem( ItemType& item, bool& found ) { bool MoreToSearch; NodeType* location; location = listData; found = false; MoreToSearch = (location != NULL); while(MoreToSearch && !found) { if(location->info.key < item.key) { location = location -> next; MoreToSearch = (location != NULL); } else if(item.key == location -> info.key) { found = true; item.key = location -> info.key; } else MoreToSearch = false; } } template NodeType* SortedType::GetCurPointer() { return currentPos; } template void SortedType :: InsertItem ( ItemType item) { bool moreToSearch ; NodeType* newNode; NodeType* predLoc = NULL; NodeType* location = listData; moreToSearch = ( location != NULL ) ; while ( moreToSearch ) { if ( location->info.key < item.key ) { predLoc = location; location = location->next; moreToSearch = ( location != NULL ) ; } else moreToSearch = false ; } newNode = new NodeType; newNode->info = item; if (predLoc == NULL) { newNode->next = listData; listData = newNode; } else { newNode->next = location; predLoc->next = newNode; } } template void SortedType::DeleteItem ( ItemType item ) { NodeType* location = listData ; NodeType* tempLocation ; if (item == listData->info) { tempLocation = location; listData = listData->next; } else { while (item != location->next->info) location = location->next; tempLocation = location->next; location->next = (location->next)->next; } delete tempLocation; } template void SortedType::ResetList ( ) { currentPos = NULL; } template void SortedType::GetNextItem (ItemType& item) { if(currentPos == NULL) currentPos = listData; else currentPos = currentPos -> next; item = currentPos -> info; } template SortedType::~SortedType() { // MakeEmpty(); } #endif