Hi, i have a problem comparing two elments, i need to check if the elemenet i want to add to my list is already exist. I'd be glad you can help me.
comparing function:
bool const IntLinkedSet::checkExistance(Number& const num) const
{
if(m_Head == NULL)
return false;
else
{
Node* tmp = m_Head;
while(tmp != NULL)
{
if( tmp -> num == &num )
return true; /// when it gets to the number that already exists, it just continue to else and doen't return true value..
else
tmp = tmp -> m_Next;
}
return false;
}
}
adding function:
bool IntLinkedSet::add(Number& num)
{
if( checkExistance(num) == false )
{
Node* tmp = new Node;
tmp ->num = #
if(m_Head == NULL)
{
m_Head = tmp;
m_Tail = m_Head;
m_Size += 1;
}
else
{
m_Tail -> m_Next = tmp;
tmp -> m_Prev = m_Tail;
m_Tail = tmp;
m_Size += 1;
}
return true;
}
else
{
cout<<"error, number already exists\n";
return false;
}
}
main:
int main()
{
Int n1(13);
Int n2(15);
Int n3(45);
Int n4(17);
Int n5(45);
IntLinkedSet list;
list.add(n1);
list.add(n2);
list.add(n3);
list.add(n4);
list.add(n5);
}
can you please explain, what does it do:
while ( n && n->num != num ) n = n->m_Next;
return ( n != NULL ); ?
The same as your function checkExistance but does it correctly and clear.