I was wondering if anyone can give me an idea on how i can modify this function to check for duplicates, if it contains a duplicate, since duplicates are not aloowed, output a message.
Any help would greatly be appreciated.
template<class Type>
void orderedLinkedListType<Type>::insertNode(const Type& newItem)
{
nodeType<Type> *current; //pointer to traverse the list
nodeType<Type> *trailCurrent; //pointer just before current
nodeType<Type> *newNode; //pointer to create a node
bool found;
newNode = new nodeType<Type>; //create the node
assert(newNode != NULL);
newNode->info = newItem; //store newitem in the node
newNode->link = NULL; //set the link field of the node
//to NULL
if(first == NULL) //Case 1
{
first = newNode;
count++;
}
else
{
current = first;
found = false;
while(current != NULL && !found) //search the list
if(current->info >= newItem)
found = true;
else
{
trailCurrent = current;
current = current->link;
}
if(current == first) //Case 2
{
newNode->link = first;
first = newNode;
count++;
}
else //Case 3
{
trailCurrent->link = newNode;
newNode->link = current;
count++;
}
}//end else
}//end insertNode
<< moderator edit: added [code][/code] tags >>