所以我寫了一個程序來插入,刪除和顯示排序後的鏈表。一切運行平穩,但當我輸入一個無效的數字(不在排序的鏈接列表)刪除,我的程序崩潰。這是我的刪除功能: -在C中排序的鏈表中刪除一個節點
struct node* remove(struct node* head_ptr, int target)
{
struct node* help_ptr, *node2del;
help_ptr = head_ptr;
if(help_ptr != NULL)
{
if(help_ptr -> data == target)
{
head_ptr = help_ptr -> next;
free(help_ptr);
return head_ptr;
}
while (help_ptr -> next != NULL)
{
if(help_ptr -> next -> data == target)
{
node2del = help_ptr -> next;
help_ptr -> next = help_ptr -> next -> next;
free(node2del);
return head_ptr;
}
help_ptr = help_ptr -> next;
}
if(help_ptr->next->data != target)
printf("\n%d is not in the list.",target);
}
return head_ptr;
}
Click here爲完整的程序。提前致謝!
感謝您的快速回復! – Shail