链接列表分配运算符C ++

时间:2016-10-16 20:55:56

标签: c++ linked-list assignment-operator

尝试为双向链表实现赋值运算符。

我认为我的部分问题是我不完全理解赋值运算符的工作原理。这是我目前的情况:

//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;

}

不确定是否需要它,但这是我的.h文件。 _head和_tail变量是虚拟元素,POINT指向第一个和最后一个元素。

这是我的.h,只是它的重要部分:

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

任何形式的帮助都会令人难以置信。在这里不知所措......

0 个答案:

没有答案