c++ - 复制构造函数后链表中的访问冲突

标签 c++ list

我在 C++ 中创建了一个链接列表,但在向列表中插入新项时出现访问冲突。

如果从不调用复制构造函数并且在整个程序执行过程中使用原始列表,则该列表将完美运行。

在按值调用函数“outList”创建列表拷贝以在其范围内管理后,我在插入方法中收到错误。

列表维护一个指向作为事件元素的 ListElement 的游标(指针)。该列表包含诸如“gotoNext”和“gotoPrior”等方法。复制构造函数的目的是创建列表的深层拷贝并保持光标在拷贝中的位置。

在阅读代码之前,这里是堆栈跟踪,如您所见,程序在仅向列表添加一个元素后崩溃。

LinkedListExample.exe!LinkedList<char>::insert(const char & item, int position) Line 71 C++
LinkedListExample.exe!LinkedList<char>::LinkedList<char>(const LinkedList<char> & src) Line 35  C++
LinkedListExample.exe!main() Line 62    C++

这是Main()

void main()
{
    LinkedList<char> testList;      // Test list
    char insertChar;
    do{
        cin >> insertChar;
        outList(testList);
        testList.insert(insertChar, 0); //0 means insert after cursor, -1 insert before.
    }
    while(insertChar != 'q')
}

这是 list

template <typename Type>
void outList(LinkedList<Type> list) 
{
    char tmp;
    if (list.empty())
       cout << "Empty list" << endl;
    else
    {
        list.gotoBeginning();
        do
        {
            tmp = list.retrieve();
            cout << tmp << " -> "; 
        }
        while (list.gotoNext());
        cout << endl;
    }
}

这是 LinkedList 复制构造函数:

template <typename Type>
LinkedList<Type>::LinkedList(const LinkedList &src){
    if(src.head == NULL){
        head = NULL;
        cursor = NULL;
    }else{
        ListElement *iterator = src.head;
        ListElement *placeHolder = src.cursor;
        int numHops = 0;
        if(iterator->next == NULL){
            numHops = 1;
        }else{
            while(iterator != placeHolder){
                numHops++;
                placeHolder = placeHolder->next;
            }
        }
        iterator = src.head;
        while(iterator != NULL){
            insert(iterator->element, 0);
            if(iterator->next != NULL){
                iterator = iterator->next;
            }else{
                break;
            }
        }
        gotoBeginning();
        if(numHops != 0){
            for(int i = 0; i < numHops; i++){
                gotoNext();
            }
        }
    }
}

这是 LinkedList 插入:

template <typename Type>
void LinkedList<Type>::insert(const Type &item, int position){
    if(head == NULL){
        ListElement *newElement = new ListElement(item, NULL);
        newElement->next = NULL;
        head = newElement;
        cursor = head;
    }else if(position == 0){
        if(head->next == NULL){
            cursor = head;
        }
        //Create a new ListElement after the current.
        ListElement *newElement = new ListElement(item, NULL);
        //Preserve link (if there is one)
        if(cursor->next != NULL){
            newElement->next = cursor->next;
        }
        cursor->next = newElement;
        cursor = newElement;
    }else if(position == -1){
        //Add a new element before the current.
        ListElement *newElement = new ListElement(item, NULL);
        if(head->next == NULL || (cursor == head && cursor != NULL)){
            //Special case. There is only one node.  Must update head pointer.
            newElement->next = head;
            head = newElement;
            cursor = newElement;
        }else{
            //Node will be put between two other nodes.
            ListElement *iterator = head;
            while(iterator->next != cursor){
                iterator = iterator->next;
            }
            newElement->next = iterator->next;
            iterator->next = newElement;
            cursor = newElement;
        }
    }//end if line 24
}

供您引用,堆栈跟踪告诉我第 71 行是崩溃点。这是代码块“插入”中的那一行...

else if(position == 0){
            if(head->next == NULL){
                cursor = head;
            }
            //Create a new ListElement after the current.
            ListElement *newElement = new ListElement(item, NULL);
            //Preserve link (if there is one)
            if(cursor->next != NULL){ //LINE 71 LINE 71 LINE 71 LINE 71
                newElement->next = cursor->next;
            }

最佳答案

在你的复制构造函数中,如果 src.head != null,那么当你第一次调用 insert 时,headcursor 不会被初始化。您可能打算将它们初始化为 NULL

关于c++ - 复制构造函数后链表中的访问冲突,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30747023/

相关文章:

c++ - 为什么在函数参数中将字符串作为常量字符串传递

C++ operator new 重载,编译错误

java - 限制迭代时删除元素

java - 获取进入的地理围栏的ID

python - 解包列表理解中元素的 "the rest"- python3to2

C++ 如何将整数和 float 等数据类型编码为字节串?

c++ - 读取文件时找到文件末尾

python : Get many list from a list

c++ - 无法打开 UART。确保它没有被其他应用程序使用

合并两个重叠列表的Pythonic方法,保留顺序