所以我写了一个插入、删除和显示排序链表的程序。一切运行顺利,但是当我输入一个无效的数字(不在排序链表中)进行删除时,我的程序崩溃了。这是我的删除功能:-
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;
}
点击这里查看完整程序。提前致谢!