C ++中的双链表的赋值运算符 [英] Assignment Operator for Doubly Linked List in C++

查看:300
本文介绍了C ++中的双链表的赋值运算符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我无法确定分配运算符的概念,或者至少成功地创建了它们.

I'm having trouble wrapping my head around the concept of an assignment operator, or at least creating them successfully.

复制构造函数对我来说不是问题;这是我的作品:

Copy Constructors aren't an issue for me; here is mine that its working:

//copy constructor
Set::Set(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;

    }

}

我正在阅读示例代码,看到有些人使用#include <algorithm>库来实现它.我尝试了一下,但是似乎根本不起作用.

I was reading example code, and saw some people used the #include <algorithm> library to implement it. I tried that instead, however does not seem to work at all.

//assignment operator
Set& Set::operator=(const Set &rhs){
    Set temp(rhs);
       std::swap(temp._head,_head);
       std::swap(temp._tail, _tail);
       return *this;

}

但是,以上代码无法正常运行.真正难以掌握如何分配运算符的概念.我认为它的工作原理与您希望将值从一个复制到另一个相同.但显然不是.如果有人可以建议我如何工作,那将是很棒的.

This above code however does not work correctly. Really struggling to grasp the concept of how assignment operators are made. I figured it would basically work the same as you want to copy values from one into another. But evidently not. If anyone could advise me on how to get that working it would be great.

关于班级的一些常规信息,有a _heada _tail指向列表的开头和结尾.虚拟元素.

Just some more general info on my class, there is a _head and a _tail that points to the start and end of the list. Dummy Elements.

对象的结构如下:

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

推荐答案

我发现您的复制构造函数存在两个问题(甚至没有尝试进一步研究):

I see two problems with your copy constructor (and not even trying to dig further):

  1. 尾巴的作用是什么?似乎不是必需的,或者使用不正确.
  2. 您的复制循环似乎在otherCurr-> next为NULL时停止.但是,您将最后一个元素上的curr-> next指向tail.这很可能意味着您要么复制集合中的1个元素,要么更糟(因为您可能无法正确查看尾部的定义来初始化tail),因此您试图复制随机位置,这将导致您的程序迟早崩溃./li>
  1. What is the purpose of tail? It doesn't seem to be needed, or is not used correctly.
  2. Your copy loop seems to stop on otherCurr->next being NULL. But you point curr->next on the last element to tail. And this very likely means that you either copy 1 element past your set or even worse (as you may not initialize tail correctly looking at its definition), you are trying to copy random location which will cause your program to crash sooner or later.

这篇关于C ++中的双链表的赋值运算符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆