c-为什么这个代码在链表的尾部插入一个节点会导致分段错误



我试图实现linkedlist函数,在列表的末尾添加节点,但它几乎在每一行都会触发分段错误。

SinglyLinkedListNode*insertNodeAtTail(SinglyLinked ListNode*head,int Data({

SinglyLinkedListNode* Node;
Node = malloc(sizeof(SinglyLinkedListNode));
SinglyLinkedListNode* current; 
current = head;

Node->data = Data;
Node->next = NULL;
if( head->next == NULL)     /* 0 element llist*/
{
head = Node;
}
else
{
//current = head;
/*
while(current->next != NULL) 
*/
while(current->next != NULL)
{
current = current->next;
}  // exit when current->next = lastNode->next = NULL
current->next = Node;
}

这可能导致seg故障:

while(current->next != NULL)  /* if current is NULL 
dereferencing it will cause segfault. */

相关内容

  • 没有找到相关文章