Thread: linked list -= operator overloading

  1. #1
    Registered User
    Join Date
    Sep 2011
    Posts
    6

    linked list -= operator overloading

    so in main I have,
    Code:
    else if(user_input == 10){
        x -= y;
        }
        else if(user_input == 11){
        y -= x;
        }
    and I am overloading the -= operator in my bag.cpp file like this,
    Code:
    void bag::operator -=(const bag& minuend)
        {
            node *cursor;
            cursor = minuend.head_ptr->link();
            while (cursor->link() != 0) {
                erase_one(cursor->data());
                cursor = cursor->link();
            }
        }

    All of my other overloads are working but I have yet to figure this one out, maybe I need some sort of list searching algorithm for this? I have one of those that I could call from here but I am not sure how I could even use it in this situation. This is homework I have and it is due tonight... if something could give me a hint or something I would really appreciate it. Thanks!
    Last edited by mrmodest; 03-09-2012 at 04:58 PM.

  2. #2
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    Operators such as -= are supposed to return a reference to *this, i.e. a bag& in this case, rather than void.

    Other than that, the code you have here is okay. You haven't asked a specific question, but if the problem is that this function does not do the right thing, then it looks to me like the problem would be in your erase_one function. I'll need to see that code to help further.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  3. #3
    Registered User
    Join Date
    Sep 2011
    Posts
    6
    Code:
    bool bag::erase_one(const value_type& target){
        node *target_ptr;
        
        target_ptr = list_search(head_ptr, target);
        if (target_ptr == NULL)
            return false; // target isn't in the bag
        target_ptr->set_data( head_ptr->data( ) );
        list_head_remove(head_ptr);
        --many_nodes;
        return true;
        }
    The -= to my understanding is not working correctly... If I say x -= y then it should remove anything that is the same in x and y and put in x what remains. But, I posted you my erase one function.

  4. #4
    Registered User
    Join Date
    Sep 2011
    Posts
    6
    My - and + work fine,

    Code:
    bag operator +(const bag& b1, const bag& b2){
        bag answer;
    
        answer += b1; 
        answer += b2;
        return answer;
        }
        
        bag operator -(const bag& b1, const bag& b2)
        {
            bag answer;
            
            answer = b1;
            answer -= b2;
            
            return answer;
        }
    and also my += works correctly,

    Code:
    void bag::operator +=(const bag& addend)
        {
        node *copy_head_ptr;
        node *copy_tail_ptr;
        
        if (addend.many_nodes > 0)
        {
            list_copy(addend.head_ptr, copy_head_ptr, copy_tail_ptr);
            copy_tail_ptr->set_link( head_ptr ); 
            head_ptr = copy_head_ptr;
            many_nodes += addend.many_nodes;
        }
        }

  5. #5
    Registered User
    Join Date
    Sep 2011
    Posts
    6
    Should I post more of my code?

  6. #6
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    Sorry for the delay, I'm only on here a couple of times a day at best. I hoped someone else would be able to help you in the mean time.

    Using operator -= inside operator - is good and is the right way to go about it. However it really makes it hard to see how it could not work while operator - does.
    I think what we need to see now, is some example for which it does not work.
    Start with the simplest cases first:
    Code:
    {} -= {} --> should give {}
    {} -= {1} --> should give {}
    {1} -= {} --> should give {1}
    {1} -= {1} --> should give {}
    Then try larger examples:
    Code:
    {1, 2} -= {} --> should give {1, 2}
    {1, 2} -= {1} --> should give {2}
    {1, 2} -= {2} --> should give {1}
    {1, 2} -= {1, 2} --> should give {}
    Now, which of those cases does not work, and what happens when it goes wrong?
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Overloading + operator
    By juice in forum C++ Programming
    Replies: 6
    Last Post: 09-30-2011, 11:03 AM
  2. Operator overloading Polynomials Doubly linked list
    By Fatima Rizwan in forum C++ Programming
    Replies: 2
    Last Post: 02-27-2010, 09:58 AM
  3. Overloading operator++
    By vopo in forum C++ Programming
    Replies: 9
    Last Post: 08-18-2008, 02:52 AM
  4. Circular Double Linked List - Logic in Operator=
    By INFERNO2K in forum C++ Programming
    Replies: 5
    Last Post: 09-30-2006, 12:45 PM
  5. Replies: 6
    Last Post: 03-02-2005, 02:45 AM