简体   繁体   English

如何访问链表的 null 指针(节点)?

[英]How do I access a null pointer (node) of a linked list?

This method is supposed to append a Node at the end of a linked list.这个方法应该是把 append 一个 Node 放在一个链表的末尾。 The method loops until it reaches the end, which is the null pointer.该方法循环直到到达末尾,即 null 指针。 But when I try to change the null pointer to a value, it crashes.但是当我尝试将 null 指针更改为一个值时,它会崩溃。 How should I fix this?我应该如何解决这个问题? (The Node pointer has a integer data and another Node variable which the current Node points to). (Node 指针有一个 integer 数据和当前 Node 指向的另一个 Node 变量)。

void appendItem(LinkedList* list, int value)
{
    Node* temp = (Node*)malloc(sizeof(Node));
    temp = list->head;

    while(temp != NULL)
    {
        temp = temp->next;
    }

    temp->data = value;
    temp->next = NULL;
}

Dereferencing NULL is prohibited.禁止取消引用NULL

Instead of that, you should manage a pointer to what should be changed.取而代之的是,您应该管理指向应该更改的内容的指针。

Also note that allocating some buffer via malloc() and overwriting the result with another value just after that causes a memory leak.另请注意,通过malloc()分配一些缓冲区并在其后用另一个值覆盖结果会导致 memory 泄漏。

One more point is that casting results of malloc() family is considered as a bad practice .还有一点是malloc()系列的强制转换结果被认为是一种不好的做法

Fixed code:固定代码:

void appendItem(LinkedList* list, int value)
{
    Node** temp = &list->head;

    while(*temp != NULL)
    {
        temp = &(*temp)->next;
    }

    *temp = malloc(sizeof(Node));
    if (*temp != NULL)
    {
        (*temp)->data = value;
        (*temp)->next = NULL;
    }
}

These lines这些行

Node* temp = (Node*)malloc(sizeof(Node));
temp = list->head;

produces a memory leak.产生 memory 泄漏。 At first a memory was allocated and its address was stored in the pointer temp and then the value of the pointer temp was overwritten by the value of the expression list->head .首先分配了 memory 并将其地址存储在指针temp中,然后指针temp的值被表达式list->head的值覆盖。 As a result the address of the allocated memory was lost.结果,分配的 memory 的地址丢失了。

After this loop在这个循环之后

while(temp != NULL)
{
    temp = temp->next;
}

the pointer temp is equal to NULL .指针temp等于NULL So a null pointer is used to access memory in these statements因此在这些语句中使用 null 指针来访问 memory

temp->data = value;
temp->next = NULL;

that invokes undefined behavior.调用未定义的行为。

The function can be defined for example the following way.例如,function 可以通过以下方式定义。

int appendItem( LinkedList *list, int value )
{
    Node *new_node = malloc( sizeof( Node ) );
    int success = new_node != NULL;

    if ( success )
    {
        new_node->data = value;
        new_node->next = NULL;

        if ( list->head == NULL )
        {
            list->head = new_node;
        }
        else
        {
            Node *current = list->head;
            while ( current->next != NULL ) current = current->next;
            current->next = new_node;
        }
    }

    return success;
}

Pay attention to that the memory allocation can fail.请注意,memory 分配可能会失败。 You need to process such a case in your function.您需要在 function 中处理这种情况。 And the caller of the function should be informed about such a situation. function 的调用者应该被告知这种情况。

Also as you allow to append new nodes to a singly-linked list then the list should be defined as a two-sided singly-linked list.此外,当您允许 append 将新节点添加到单链表时,该列表应定义为双边单链表。 That is the list should keep two pointers: one pointer to the head node and other pointer to the tail node.也就是说,列表应该保留两个指针:一个指向头节点的指针,另一个指向尾节点的指针。 Otherwise appending a node to the list will be inefficient.否则将节点附加到列表将是低效的。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使用单个指针从链表中删除节点? - How can I delete an node from a linked list with a single pointer? 为什么我无法将分配了 memory 的结构节点指针分配给先前值为 null 的结构节点指针以生成链表? - Why i am not able to assign a struct node pointer with allocated memory to a struct node pointer with previous value of null for making a linked list? 如何删除指向链表的指针中的节点? - How to remove a node in a pointer to linked list? 如果同一索引中的下一个节点不是 NULL,我如何删除单链表链接 hash 表的第一个节点? - How do i delete the first node of singly linked list chaining hash table if the next node in the same index is not NULL? 如何访问由 void 指针指向的链表 - How to access a linked list pointed to by a void pointer 在我的单链列表实现中,即使我为要释放的节点分配了内存,但指向该节点的指针不是NULL,为什么仍然如此? - In my Singly linked List implementation why is it even though I allocated memory for the node to be freed, the pointer to the Node isn't NULL? 如何通过链接列表节点将信息保存到结构? - how do I save info to a struct through a Linked List Node? 如何在链表的开头插入节点? - How do I insert a node at the beginning of a linked list? 如何在另一个链表中获取链表的奇数索引节点? 我不想使用双指针 - How can I get the odd indexed nodes of a linked list in an another linked list?I do not want to use double pointer 如何使用给定的递归在单链表中插入节点:1. 指向头节点的指针 2. 插入新节点的索引 - How can I insert a node in a singly linked list using recursion given: 1. a pointer to the head node 2. the index to insert the new node
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM