简体   繁体   English

对链表副本构造函数和赋值运算符使用copy()方法

[英]Using a copy() method for a linked list copy constructor and assignment operator

I'm trying to implement a copy constructor for a linked list. 我正在尝试为链表实现副本构造函数。 I've written a copy method that returns a list that's going to be used for the copy constructor and overloading the assignment operator: 我编写了一个copy方法,该方法返回一个列表,该列表将用于副本构造函数并重载赋值运算符:

template<class T>
SinglyList<T> SinglyList<T>::copy(Node *u) {
        SinglyList<T> newList;
        Node *current = u;
        if (current->next==NULL) {
          newList.add(current->x);
        } else while (current!=NULL) {
            newList.add(current->x);
            current = current->next;
            }
        return newList;
}

With the add() method used above here: 使用上面在这里使用的add()方法:

template<class T>
void SinglyList<T>::add(T x) {
    Node *u = new Node(x);
    if (n == 0) {
        head = u;
    } else {
        tail->next = u;
    }
    tail = u;
    n++;
}

I've been trying to implement the copy constructor as so: 我一直在尝试实现复制构造函数,如下所示:

template<class T>
SinglyList<T>::SinglyList(const SinglyList<T> &a) {
    this->copy(a.head); //Does this not work?
}

I run the code as such in main(): 我在main()中这样运行代码:

int main() {
  SinglyList<int> test;
  for (int i=0; i<5; i++)
    test.add(i);
  test.print(); //This outputs 0 1 2 3 4
  SinglyList<int> test2 = test;
  test2.print(); //This should output 0 1 2 3 4 but outputs a bunch of garbage numbers
  return 0;
}

Then it crashes. 然后它崩溃了。 I'm not entirely sure what the problem is. 我不确定是什么问题。 Is it with the copy constructor or the copy method? 是使用复制构造函数还是复制方法?

In regards to overloading the assignment operator, using the copy method doesn't work either but running the code itself in the overload works? 关于重载赋值运算符,使用copy方法也不起作用,但是在重载中运行代码本身有效吗?

template<class T>
SinglyList<T>& SinglyList<T>::operator=(const SinglyList<T> &b) {
    //this->copy(b.head); <---This doesn't work
    Node *current = b.head;
    if (current->next==NULL) {
        this->add(current->x);
    } else while (current!=NULL) {
        this->add(current->x);
        current = current->next;
    }
    return *this;
}

Accompanying code of the class: 该类的随附代码:

template<class T>
class SinglyList {
protected:
    class Node {
    public:
        T x;
        Node *next;
        Node(T x0) {
            x = x0;
            next = NULL;
        }
    };
    Node *head;
    Node *tail;
    int n;
    SinglyList<T> copy(Node*);
public:
    SinglyList();
    SinglyList(const SinglyList<T>&);
    ~SinglyList() {
        Node *u = head;
        while (u != NULL) {
            Node *w = u;
            u = u->next;
            delete w;
        }
    };
    void add(T);
    SinglyList<T>& operator=(const SinglyList<T>&);
    void print();
};

Disclaimer: Some of this code was lifted from Open Data Structures and the HW was to modify the code to add extra features to the existing code. 免责声明:此代码中的某些是从开放数据结构中删除的,硬件是修改代码以向现有代码添加额外的功能。

There are a few problems, the biggest which is an infinite recursion. 有一些问题,最大的问题是无限递归。

Your copy-constructor call the copy function which returns a new list by value, meaning it will be copied and the copy-constructor will be called. 您的复制构造函数调用copy函数,该函数按值返回一个新列表,这意味着将对其进行复制并调用复制构造函数。 And so on and so on. 等等等等。 This issue would have been easily detected using a debugger . 使用调试器可以很容易地检测到此问题。 I suggest you take some time to learn how to debug your programs . 我建议您花一些时间来学习如何调试程序

With proper initialization of the member variables, you could possibly use the assignment operator (as you show it) to implement the copy-constructor, like *this = a; 通过正确初始化成员变量,您可以使用赋值运算符(如您所示)来实现复制构造函数,例如*this = a; .

However, I would rather recommend that you modify the copy function to copy from the other list into this list instead of creating a new list and returning it. 但是,我宁愿建议您修改copy功能以从另一个列表复制到列表中,而不是创建一个新列表并返回它。


About that assignment operator... You have to think about the case when the current list already have nodes, you have to remove them first. 关于该赋值运算符...您必须考虑当前列表中已经节点的情况,您必须首先将其删除。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM