为什么将节点插入链表末尾的链表时会出现分段错误?

时间:2021-03-18 00:53:29

标签: pointers linked-list

// *****************ALPHEBETICAL 插入 **************************** ***

            struct node *curr;
            struct node *trail;
            
            
            
            curr = head;
            trail = NULL;
            
            while (strcmp(curr->name, new_node->name) < 0 )
            {
                trail  = curr;
                curr = curr->next;
                
            }
            
            // if head insertion
            
            if (curr == head)
            {
                head = new_node;
                new_node->next = curr;
            }
            
            // if mid insertion
            
            else if ((curr != 0) && (curr != head) )
            {
                new_node->next = curr;
                trail->next = new_node;
            }
            
            // if tail insertion
            
            
            
            **else if (curr == NULL)
            {
                trail->next = new_node;
                new_node->next = NULL;
            }**
                
            
        }
<块引用>

有人可以解释为什么我在最后一种情况下出现分段错误 - 当尝试在列表末尾插入时吗?

0 个答案:

没有答案
相关问题