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

Linked List Copy Constructor | Undefined Behavior

本文关键字:未定义 复制 构造函数 链表      更新时间:2023-10-16

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

#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 constructorn";
    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 assignmentn";
}
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;
}

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

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

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

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

template<typename T>
LinkedList<T>::LinkedList(const LinkedList<T> &list)
    : head{ nullptr }, tail{ nullptr }, size{0}
{
    std::cout << "In the copy constructorn";
   Node* curNode = list.head;
    while (curNode != nullptr)
    {
      this->push(curNode->data);
        curNode = curNode->next;
    }
}

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