链接列表复制构造函数|未定义的行为

时间:2017-06-21 15:50:54

标签: c++ linked-list copy-constructor deep-copy

我一直在模板链接列表上构建,并且最近实现了一个用户定义的复制构造函数来处理我的链接列表的深层副本。我当前的解决方案编译得很好但是当我执行时,没有显示任何内容。

#include <iostream>

template<typename T>
class LinkedList
{
private:
    struct Node
    {
        T data;
        Node* next;
    };

    Node* head;
    Node* tail;
    int size;

public:
    LinkedList();
    LinkedList(const LinkedList<T> &list);
    ~LinkedList();
    LinkedList<T>& operator=(const LinkedList<T> list);
    int getSize() const;
    void display() const;
    void push(const T &data);
    void insert(const int pos, const T &data);
    void remove(const int pos);

};

template<typename T>
LinkedList<T>::LinkedList() : head{ nullptr }, tail{ nullptr }, size{ 0 }
{
}

// TODO: User-defined copy constructor
template<typename T>
LinkedList<T>::LinkedList(const LinkedList<T> &list)
    : head{ nullptr }, tail{ nullptr }, size{list.size}
{
    std::cout << "In the copy constructor\n";
    if (list.head == nullptr)
    {
        return;
    }
    else
    {
        Node* curNode = new Node{list.head->data, nullptr};
        head = curNode; // sets the head member variable to first node in Linked List

        Node* curListNode = list.head;      

        while (curListNode->next != nullptr)
        {
            curNode->next = new Node{curListNode->next->data, nullptr};

            curListNode = curListNode->next;
            curNode = curNode->next;
        }

        curNode->next = new Node{curListNode->next->data, nullptr};
        tail = curNode->next;
    }

}

template<typename T>
LinkedList<T>::~LinkedList()
{
    Node* prevNode = head;
    Node* curNode = head;

    while(curNode != nullptr)
    {
        prevNode = curNode;
        curNode = curNode->next;

        delete prevNode;
    }

    head = nullptr;
    tail = nullptr;
}

template<typename T>
LinkedList<T>& LinkedList<T>::operator=(const LinkedList<T> list)
{
    // make a copy of each node - much like the copy constructor
    std::cout << "In the overloaded assignment\n";
}

template<typename T>
int LinkedList<T>::getSize() const
{
    return size;
}

template<typename T>
void LinkedList<T>::display() const
{
    Node* curNode = head;

    while (curNode != nullptr)
    {
        std::cout << curNode->data << '\n';
        curNode = curNode->next;
    }

    std::cout << '\n';
}

template<typename T>
void LinkedList<T>::push(const T &data)
{
    Node* newNode = new Node{data, nullptr};

    if (size == 0)
    {
        head = newNode;
        tail = newNode;
    }
    else 
    {
        tail->next = newNode;
        tail = newNode;
    }

    ++size;
}

template<typename T>
void LinkedList<T>::insert(const int pos, const T &data)
{
    if (pos < 0 || pos > size)
    {
        throw "Index is out of range!";   
    }
    else if (pos == size)
    {
        push(data);   
    }
    else
    {
        Node* newNode = new Node{data, nullptr};
        Node* prevNode = head;
        Node* curNode = head;

        int i = 0;
        while (i != pos)
        {
            prevNode = curNode;
            curNode = curNode->next;
            ++i;
        }

        prevNode->next = newNode;
        newNode->next = curNode;

        ++size;
    }
}

template<typename T>
void LinkedList<T>::remove(const int pos)
{
    if (pos < 0 || pos > size)
    {
        throw "Index is out of range!";   
    }
    else if (size == 0)
    {
        throw "List is empty!";   
    }
    else
    {
        Node* prevNode = head;
        Node* curNode = head;

        int i = 1;
        while (i != pos)
        {
            prevNode = curNode;
            curNode = curNode->next;
            ++i;
        }

        prevNode->next = curNode->next;
        delete curNode;

        if (pos == size)
        {
            tail = prevNode;   
        }

        --size;
    }
}

int main()
{
    LinkedList<int> list;

    list.push(1);
    list.push(3);
    list.push(6);

    list.display();

    LinkedList<int> copyList{list};

    copyList.display();

    return 0;
}

当我将LinkedList<int> list语句添加到copyList时,第一个main()甚至不会显示。但是,当我注释掉两个copyList语句时,list会正常显示。

我添加了print语句,以查看是否正在调用复制构造函数或operator=函数,并且当copyList语句添加到我的程序时,这些函数都不会打印到控制台(代码编译得很好。)

我可以使用任何帮助来帮助诊断导致此行为的原因。

1 个答案:

答案 0 :(得分:0)

像这样的简单复制构造函数就足够了:

template<typename T>
LinkedList<T>::LinkedList(const LinkedList<T> &list)
    : head{ nullptr }, tail{ nullptr }, size{0}
{
    std::cout << "In the copy constructor\n";
   Node* curNode = list.head;

    while (curNode != nullptr)
    {
      this->push(curNode->data);
        curNode = curNode->next;
    }

}

请注意,这只是为了在当前列表中插入元素而重写的display方法。