所以我写了一个程序来插入,删除和显示排序后的链表。一切运行平稳,但当我输入一个无效的数字(不在排序的链接列表)删除,我的程序崩溃。这是我的删除功能: -在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