C++ 在双链接列表的尾部插入

C++ 在双链接列表的尾部插入,c++,linked-list,nodes,doubly-linked-list,C++,Linked List,Nodes,Doubly Linked List,我第一次使用链表,必须创建一个可以在双链表末尾插入节点的函数。到目前为止我有 void LinkedList::insertAtTail(const value_type& entry) { Node *newNode = new Node(entry, NULL, tail); tail->next = newNode; tail = newNode; ++node_count; } Node类接受要存储的值、要指向的下一个指针的值以及按该顺序的

我第一次使用链表,必须创建一个可以在双链表末尾插入节点的函数。到目前为止我有

void LinkedList::insertAtTail(const value_type& entry) {
    Node *newNode = new Node(entry, NULL, tail);
    tail->next = newNode;
    tail = newNode;
    ++node_count;
}
Node类接受要存储的值、要指向的下一个指针的值以及按该顺序的上一个指针的值。每当我尝试在此处插入节点时,都会收到一个错误,指出存在未处理的异常,并且在写入位置0x00000008时存在访问冲突

我不完全确定这里出了什么问题,但我假设这与基于错误消息取消引用空指针有关。如果能帮我解决这个问题,我将不胜感激

编辑:


我应该早点澄清,tail是指向列表中最后一个节点的指针。Tail->next访问最后一个节点的下一个变量,该变量在函数运行之前指向NULL,但在执行之后应该指向创建的新节点。

顺便说一句,如果不知道插入操作(实际上是追加而不是插入)之前的列表状态,很难判断是什么导致了错误

很可能您没有处理附加到空列表的初始情况。基本算法是一个空列表,由一个空的头指针指示,其他一切都是不确定的:

def append (entry):
    # Common stuff no matter the current list state.

    node = new Node()
    node->payload = entry
    node->next = NULL

    # Make first entry in empty list.

    if head = NULL:
        node->prev = NULL
        head = node
        tail = node
        return

    # Otherwise, we are appending to existing list.

    next->prev = tail
    tail->next = node
    tail = node

顺便说一句,在插入操作之前,如果不知道列表的状态(实际上是追加而不是插入),很难判断是什么导致了错误

很可能您没有处理附加到空列表的初始情况。基本算法是一个空列表,由一个空的头指针指示,其他一切都是不确定的:

def append (entry):
    # Common stuff no matter the current list state.

    node = new Node()
    node->payload = entry
    node->next = NULL

    # Make first entry in empty list.

    if head = NULL:
        node->prev = NULL
        head = node
        tail = node
        return

    # Otherwise, we are appending to existing list.

    next->prev = tail
    tail->next = node
    tail = node
尾巴最初指向哪里?如果为NULL,则在尝试插入第一个元素时将取消对NULL指针的引用

在解引用之前测试tail是否有帮助

void LinkedList::insertAtTail(const value_type& entry) {
    Node *newNode = new Node(entry, NULL, tail);
    if (tail)
        tail->next = newNode;
    tail = newNode;
    ++node_count;
}
如果tail为null且offsetofNode,则next为8,这将解释访问冲突,因为tail->next将位于地址0x00000000+8,即0x00000008,因此分配给tail->next将尝试在该地址写入内存,这正是您看到的错误。

tail最初指向哪里?如果为NULL,则在尝试插入第一个元素时将取消对NULL指针的引用

在解引用之前测试tail是否有帮助

void LinkedList::insertAtTail(const value_type& entry) {
    Node *newNode = new Node(entry, NULL, tail);
    if (tail)
        tail->next = newNode;
    tail = newNode;
    ++node_count;
}

如果tail为null且offsetofNode,则next为8,这将解释访问冲突,因为tail->next将位于地址0x00000000+8,即0x00000008,因此分配给tail->next将尝试在该地址写入内存,这正是您看到的错误。

假设您的LinkedList同时具有头和尾,也许可以试试:

void LinkedList::insertAtTail(const value_type& entry) 
{
    Node *newNode = new Node(entry, NULL, tail);
    if (tail)
        tail->next = newNode;
    tail = newNode;
    if (!head)
        head = newNode;
    ++node_count;
}

只需在黑暗中拍摄一张照片

假设你的LinkedList既有头部也有尾部,可以尝试:

void LinkedList::insertAtTail(const value_type& entry) 
{
    Node *newNode = new Node(entry, NULL, tail);
    if (tail)
        tail->next = newNode;
    tail = newNode;
    if (!head)
        head = newNode;
    ++node_count;
}

只是在黑暗中拍摄

向我们展示:LinkedList和Node类,因为您的第一篇文章中没有太多上下文。tail和tail->next指向新节点是否有问题?看起来像循环引用,但我可能错了。你的尾巴最初是空的吗?在tail->next中,除非它已经指向第一个元素,否则无法取消对它的引用操作顺序看起来是正确的。您传递的参数指示正确的节点初始化。仅此而已,我会冒昧地猜测您的尾部指针已损坏,或者value_类型的复制构造函数正在内存中跺脚。向我们展示更多的代码plz。想想如果尾巴在尾巴前面->下一个任务。同样,这个列表上的头分配在哪里?这个列表是纯空的,尾插入是第一个??可能也需要这样做。@Thomas,当tail->next设置为指向新节点时,tail引用了前一个tail。告诉我们:LinkedList和Node类,因为在你的第一篇文章中没有太多上下文。tail和tail->next指向新节点有什么问题吗?看起来像循环引用,但我可能错了。你的尾巴最初是空的吗?在tail->next中,除非它已经指向第一个元素,否则无法取消对它的引用操作顺序看起来是正确的。您传递的参数指示正确的节点初始化。仅此而已,我会冒昧地猜测您的尾部指针已损坏,或者value_类型的复制构造函数正在内存中跺脚。向我们展示更多的代码plz。想想如果尾巴在尾巴前面->下一个任务。同样,这个列表上的头分配在哪里?这个列表是纯空的,尾插入是第一个??可能也需要。@Thomas,当tail->next设置为指向新节点时,tail引用了前一个tail。谢谢大家的帮助!结果是尾巴指向空。当我意识到这是一个简单的解决办法。再次感谢!谢谢大家的帮助!结果是尾巴指向空。当我意识到这是一个简单的解决办法。再次感谢!