0

Trying to implement an assignment operator for a doubly linked list class.

I think part of my problem with this is I do not completely understand how assignment operators work. Here is what I have currently:

//assignment operator
Set& Set::operator=(const Set &rhs){

    _head = rhs._head;
        _tail = rhs._tail;

        //null, basically this object is 0
        if(rhs._head == NULL){
            _head = NULL;
            _tail = NULL;
            _size = 0;
        }else{
            _head = new Elem(*rhs._head);
            _tail = new Elem(*rhs._tail);
            _size = rhs._size;

            Elem *prev = NULL;
            Elem *curr = _head;
            Elem *otherCurr = rhs._head;
            int counter = 0;
            while(otherCurr->next != NULL){
                curr->next = new Elem(*otherCurr->next);
                curr->next->prev = curr;


                curr = curr->next;
                otherCurr = otherCurr->next;
            }

            //now that we are done lets setup the tail
            _tail->prev = curr;
            curr->next = _tail;



        }

        return *this;

}

Not sure if it will be needed but here is my .h file. The _head and the _tail variables are dummy Elements, and POINT to the first and last elements.

Here is my .h, well just the important part of it:

struct Elem {
        ELEMENT_TYPE info;
        Elem *prev, *next;
    };
    Elem *_head, *_tail;
    int _size;

Any sort of help would be incredible. At a loss here...

Tom Billy
  • 9
  • 1
  • 2
    If you have a working `insert()`, `append()`, or something similar, the assignment operator is nothing more than `erase()` all existing contents of `this`, then using insert/append/whatever to copy the nodes from the other list, one by one. – Sam Varshavchik Oct 16 '16 at 20:57
  • 1
    [Possible duplicate](http://stackoverflow.com/questions/39950635/c-linked-list-assignment-operator) – PaulMcKenzie Oct 16 '16 at 21:18
  • You know, I think we just saw [this exact broken code](http://stackoverflow.com/questions/40074451/assignment-operator-for-doubly-linked-list-in-c) being used in a copy constructor in a different question from a different user. Maybe you should hook up with them? Then again, probably not. They other guy's convinced the code works. – user4581301 Oct 16 '16 at 21:38
  • I do have an insert(), I guess I will have to figure out some sort of way to erase it – Tom Billy Oct 16 '16 at 21:40
  • @user4581301 , just trying to get help on this code. That's all man. Don't have to go from question to question salting on people – Tom Billy Oct 16 '16 at 21:41
  • pretty negative experience on this site honestly. Just trying to get help on how to get this working and instead all these different police officers show up. – Tom Billy Oct 16 '16 at 21:42
  • 1
    @TomBilly -- I gave you a link to how to get an assignment operator to work, provided you have a copy constructor and destructor that works. I would hardly call that a "negative experience". – PaulMcKenzie Oct 16 '16 at 21:50
  • Tom Billy, the best help you are going to get on this particular bit of code is don't use it. I don't know where you folk got it, but it's so poorly conceived that it's not worth fixing. Throw it away. – user4581301 Oct 16 '16 at 22:03

0 Answers0