I am working on a linked list project and I have two functions that I can't get to work right.
First I have a insert function that adds an element to the list. But the function first wants to check if the element is already in the list. It should do that by using the bool function contain(). That should return true if the element is already in the list
my insert function:
void StringSet::insert(string element)
{
NodePtr temp =head;
if (contains("element") == true)
{
return;
}
if(head == NULL)
{
head = new StringNode;
head->setData(element);
head->setLink(NULL);
}
else
{
temp = new StringNode;
temp->setData(element);
temp->setLink(head);
head = temp;
}
}
And my contain function:
bool StringSet::contains(string element)
{
NodePtr temp = head;
while(temp != NULL)
{
if (temp->getData() == element)
{
cout<<"This country has already been registered!"<<endl;
return true;
}
temp = temp->getLink();
}
}
getLink()
defined?contains
looks ok (except that it's missingreturn false
. – RedXcontains("element")
is vastly different fromcontains(element)
. – molbdnilo