Problem with a list operation

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • brixton
    New Member
    • Nov 2006
    • 35

    #1

    Problem with a list operation

    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:

    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;
    }
    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 :)
  • JosAH
    Recognized Expert MVP
    • Mar 2007
    • 11453

    #2
    Note that this line:
    Code:
    Set a = *this;
    does not involve the operator= function. I think your code suffers a bit from
    incorrect value sematics. The operator- in A-B should result in a new object
    containing the difference of A and B while A and B themselves should stay
    untouched. For the first line (see above) you need a copy constructor that
    does a deep copy, i.e. all relevant members of the class should be a new copy
    of the original one too.

    kind regards,

    Jos

    Comment

    Working...