Hello,
I'm having issues dealing with lists, in this case sets of numbers.
I'm trying to get the difference between two lists, say S1 and S2.
So "S3 = S1 - S2" in my main file will produce a new set with the difference between the two. The "=" and "-" operator have been written by me.
The problem is this: S1 before the operation and S1 just after the operation are not the same. So I'm messing something up. Here is my "-" operator:
I should mention that if I check what the "this"-set is at the end of the subtraction function, it looks just fine, but as soon as I'm back in the main file it's messed up.
Big thanks in advance if anyone is bored enough to help me :)
I'm having issues dealing with lists, in this case sets of numbers.
I'm trying to get the difference between two lists, say S1 and S2.
So "S3 = S1 - S2" in my main file will produce a new set with the difference between the two. The "=" and "-" operator have been written by me.
The problem is this: S1 before the operation and S1 just after the operation are not the same. So I'm messing something up. Here is my "-" operator:
Code:
Set Set::operator- (const Set& b)
{
Set tempSet;
Set a = *this;
Node* a_temp = a.header;
Node* b_temp = b.header;
Node* this_temp = tempSet.header;
while(a_temp!=0 && b_temp!=0)
{
if (a_temp->value == b_temp->value)
{
a_temp = a_temp->next;
b_temp = b_temp->next;
}
else if (a_temp->value < b_temp->value)
{
if (tempSet.header==0)
{
tempSet.header = new Node(a_temp->value, tempSet.header);
this_temp = tempSet.header;
a_temp = a_temp->next;
}
else
{
this_temp->next = new Node(a_temp->value, this_temp->next);
this_temp = this_temp->next;
a_temp = a_temp->next;
}
}
else if (a_temp->value > b_temp->value)
{
b_temp = b_temp->next;
}
}
while (a_temp!=0)
{
this_temp->next = new Node(a_temp->value, this_temp->next);
this_temp = this_temp->next;
a_temp = a_temp->next;
}
return tempSet;
}
Code:
And the "=" operator in case that should be the issue:
Set& Set::operator=(const Set& b)
{
if ((this == &b) || (b.header==0))
{
return *this;
}
Node* temp = header;
while (temp!=0)
{
header = temp -> next;
delete temp;
temp = header;
}
Node* b_temp = b.header;
temp = header;
header = new Node(b.header->value, header);
b_temp = b_temp->next;
while(b_temp != 0)
{
insert(b_temp->value);
b_temp = b_temp -> next;
}
return *this;
}
Big thanks in advance if anyone is bored enough to help me :)
Comment