C 每当我试图调用特定函数时,就会出现分段错误
当我尝试使用以下功能删除给定节点后的节点时,出现分段错误 我也搜索过类似的问题,解决方案是在标题处检查NULL,我已经这样做了C 每当我试图调用特定函数时,就会出现分段错误,c,linked-list,singly-linked-list,C,Linked List,Singly Linked List,当我尝试使用以下功能删除给定节点后的节点时,出现分段错误 我也搜索过类似的问题,解决方案是在标题处检查NULL,我已经这样做了 struct node *delete_aft(struct node *start) { struct node *ptr,*nextptr; int x; printf("\nEnter the previous element of the element to be deleted "); scanf("%d",&x);
struct node *delete_aft(struct node *start)
{
struct node *ptr,*nextptr;
int x;
printf("\nEnter the previous element of the element to be deleted ");
scanf("%d",&x);
ptr=start;
ptr->next=nextptr;
while(ptr!=NULL&&ptr->next!=NULL)
{
if(ptr->data==x)
{
ptr->next=nextptr->next;
free(nextptr);
goto exit;
}
else
{
ptr=nextptr;
nextptr=nextptr->next;
}
}
if(ptr->next==NULL)
printf("\n Element not found");
exit:return start;
}
我对我所做更改的代码进行了注释
struct node *delete_aft(struct node *start)
{
struct node *ptr,*nextptr;
int x;
printf("\nEnter the previous element of the element to be deleted ");
scanf("%d",&x);
ptr = start;
while (ptr != NULL && ptr->next != NULL)
{
if (ptr->data == x)
{
// Make temporary copy of ptr->next.
nextptr = ptr->next;
// Set ptr->next to the node after ptr->next, i.e. ptr->next->next.
ptr->next = nextptr->next;
free(nextptr);
goto exit;
}
else
{
// Search the next node.
ptr = ptr->next;
}
}
// If we reach this point, then x was not found, or x was the last node.
if(ptr->next == NULL)
printf("\n Element not found");
exit:return start;
}
“特定功能”请更详细。。。。好。。。指定哪些函数。当start==NULL时,这里会发生什么?ptr=启动;ptr->next=nextptr;究竟是哪一行导致了分段错误?@alk我试图编写一个函数delete_aft(start);它从标题遍历链表,当找到给定元素时,它删除该元素的下一个元素。我的问题中也给出了完整的函数。请提供一个,或在调试器中逐行运行代码,以了解出了什么问题