单链接列表中的重载赋值运算符

时间:2018-10-29 19:23:42

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

我正在学习链表。我创建了一个模板实现,其中包含构造函数,插入程序,析构函数,副本构造函数和重载的赋值运算符。问题是我的测试程序在重载赋值运算符后不输出任何东西。

对于我的赋值运算符,我使用Clear()函数在复制之前完全清除列表。我将其放在析构函数中,并检查它是否可以正常工作。我还检查了我的复制构造函数,它也工作正常。

文件node.h:定义节点构件

#include <iostream>
using namespace std;

template <typename T>
struct Node{
    T _item;
    Node<T>* _next;

    Node() {
        _item = T();
        _next = NULL;
    }

    Node(T item){
        _item = item;
        _next = NULL;
    }

    // Print the value of a node
    friend std::ostream& operator <<(std::ostream& outs, const Node<T> &printMe){
        outs << "[" << printMe._item << "]";
        return outs;
    }
};

文件list.h:定义链接列表模板

#include "node.h"

template <class T>
class List {
public:

    // default constructor
    List();

    // Destructor
    ~List();

    // Copy constructor
    List(const List<T> &copyThis);

    // Overloading assignment operator
    List& operator =(const List& RHS);

    // Insert i to the head of the linked list
    Node<T>* InsertHead(T i);

    // Clear a linked list
    void Clear();

    // Overload the output operator to print the list
    template <class U>
    friend ostream& operator <<(ostream& outs, const List<U>& l);

private:
    Node<T>* head;
};

此标头还提供了以下成员函数的实现:

template <class T>
List<T>::List(){
    head = NULL;
}

template <class T>
List<T>::~List(){
    Clear();
}

template <class T>
List<T>::List(const List<T> &copyThis){
    if (copyThis.head == NULL)
        head = NULL;

    else {
        // Create walker for the original linked list
        Node<T>* walker = copyThis.head->_next;

        // Create new head node for new linked list
        head = new Node<T>(copyThis.head->_item);

        // Create new walker for new linked list
        Node<T>* new_walker = head;

        // Iterate walker and new walker and copy each item in the original list to new linked list
        while (walker!= NULL) {
            new_walker->_next = new Node<T>(walker->_item);
            walker = walker->_next;
            new_walker = new_walker->_next;
        }
    }
}

template <class T>
List<T>& List<T>::operator =(const List<T>& RHS){   // DOESN'T WORK
    if (this != &RHS) {
        this->Clear();
        *this = List<T>(RHS); 

    }
    return *this;
}

template <class T>
Node<T>* List<T>::InsertHead(T i){
    Node<T>* temp = new Node<T>(i);
    temp->_next = head;
    head = temp;
    return head;
}

// Clear a linked list
template <class T>
void List<T>::Clear(){
    Node<T>* current = head;
    Node<T>* next = new Node<T>;

    while (current != NULL) {
        next = current->_next;
        delete current;
        current = next;
    }

   head = NULL;
}

template <class U>
ostream& operator <<(ostream& outs, const List<U>& l){
    Node<U>* walker = l.head;

    while(walker != NULL){
        outs << *walker;
        outs << "->";
        walker = walker->_next;
    }

    outs << "|||";

    return outs;
}

文件main.cpp:测试类

#include <iostream>
#include "list.h"
using namespace std;

int main() {
    List<int> a;

    a.InsertHead(17);
    a.InsertHead(35);
    a.InsertHead(6);
    a.InsertHead(54);
    a.InsertHead(6);
    cout << a <<endl;;

    List<int> b;
    b.InsertHead(3);
    b.InsertHead(2);
    cout << b <<endl;;

    a = b;

    cout << a <<endl;        // PROBLEM: NOTHING IS DISPLAYED
    cout << b <<endl;
}

我当前遇到的问题是重载赋值运算符函数。下面是我从复制构造函数复制整个执行并运行的时候。

template <class T>
List<T>& List<T>::operator =(const List<T>& RHS){
    if (this != &RHS) {
        this->Clear();
        if (copyThis.head == NULL)
            head = NULL;

        else {
            // Create walker for the original linked list
            Node<T>* walker = copyThis.head->_next;

            // Create new head node for new linked list
            head = new Node<T>(copyThis.head->_item);

            // Create new walker for new linked list
            Node<T>* new_walker = head;

            // Iterate walker and new walker and copy each item in the original list to new linked list
            while (walker!= NULL) {
                new_walker->_next = new Node<T>(walker->_item);
                walker = walker->_next;
                new_walker = new_walker->_next;
            }
    }
    return *this;
}

此输出为:

2->3->|||

但是,当我简化如下代码时,它不会输出任何内容:

template <class T>
    List<T>& List<T>::operator =(const List<T>& RHS){
        if (this != &RHS) {
            this->Clear();
            *this = List<T>(RHS); 

        }
        return *this;
    }

谁能告诉我为什么它不起作用以及如何有效地简化它?我真的很感激。

1 个答案:

答案 0 :(得分:5)

问题

赋值运算符由于堆栈溢出而停止所有操作。

实际上,您对赋值运算符的实现本身使用了赋值运算符,因此它以递归方式调用自身,直到堆栈用尽:

@RequestMapping(value = "/register", method = RequestMethod.POST)
public ResponseEntity register(@RequestBody final SomeDTO someDTO, final HttpServletRequest request, final Errors errors) {}

解决方案

  1. 重写运算符,使其不会自行调用。
  2. 可能会克隆每个节点,因此要避免2个列表共享同一节点,并且第一个释放其节点的列表会导致另一个指针悬而未决。
  3. 不相关,但请避免在标头中使用名称空间。这是以后的极坏习惯。

其他提示 This article为操作员重载建议了一些好的且优雅的做法。对于赋值运算符,它建议使用复制构造函数(如您尝试那样),但是要代替赋值(您的问题),而是要交换(待验证,但是在您的情况下交换头肯定可以解决问题)。