void deleteat(int pos)
{
struct node *temp,*temp1;
//temp=(struct node*)malloc(sizeof(struct node));
temp1=temp=head;
int i;
if(pos==0)
{
temp=head->next;
free(head);
head=temp;
}
else
{
for(i=0;i<pos;i++)
{
temp1=temp;
temp=temp->next;
}
temp1->next=temp->next;
temp->next=NULL;
free(temp);
}
}
// Fucntion calls in the main
deleteat(4);
deleteat(1);
deleteat(6);
I submitted a similar code on an online judge, but it got accepted there, while running the same code on my linux machine gives the segmentation fault (core dumped). I know this question has been asked many times, but I couldn't find the error in my code.
While you iterating list you don't check if temp or temp1 are valid pointers. Some of them could be null
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments