每当我尝试调用特定函数时出现分段错误
Segmentation fault whenever I tried to call specific functions
每当我尝试使用以下功能删除给定节点之后的节点时出现分段错误
我已经搜索过类似的问题,解决方案是在 header 处检查 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);
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;
}
每当我尝试使用以下功能删除给定节点之后的节点时出现分段错误
我已经搜索过类似的问题,解决方案是在 header 处检查 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);
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;
}