function to delete a node from linked list at nth position giving segmentation fault

Charu Bansal
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.

Marcel

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.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Segmentation Fault 11 Linked List Node Struct

From Dev

Segmentation fault when free the linked list node

From Dev

Segmentation fault when adding node to linked list

From Dev

Segmentation fault when creating linked list with function

From Dev

Segmentation fault when creating linked list with function

From Dev

Linked list function causing segmentation fault

From Dev

Why is this simple linked list program giving segmentation fault?

From Dev

segmentation fault linked list

From Dev

C - Node in a linked list somehow modified, results in segmentation fault

From Dev

Linked List in C - Segmentation Fault

From Dev

C segmentation fault linked list

From Dev

C segmentation fault linked list

From Dev

Linked list implementation segmentation fault

From Dev

Segmentation fault in linked list creation

From Dev

Segmentation Fault (Variant of Linked List)

From Dev

Segmentation fault in linked list C

From Dev

Pop Function In Linked List Stack Results in Segmentation Fault- C

From Dev

Segmentation fault - How to write and read a linked list from a pipe

From Dev

Insert a Node at Nth position in Doubly linked list using C

From Dev

Insert a Node at Nth position in Doubly linked list using C

From Dev

Basic Bool function giving segmentation fault

From Dev

Linked-list node delete function

From Dev

Segmentation fault when printing the linked list

From Dev

Segmentation Fault(Core Dump) Linked List

From Dev

Segmentation fault in singly linked list C program

From Dev

Segmentation fault when deleting linked list

From Dev

Segmentation fault while reading a file into a linked list

From Dev

Doubly Linked List insertion Segmentation Fault - C

From Dev

linked list segmentation fault fixed with cerr

Related Related

  1. 1

    Segmentation Fault 11 Linked List Node Struct

  2. 2

    Segmentation fault when free the linked list node

  3. 3

    Segmentation fault when adding node to linked list

  4. 4

    Segmentation fault when creating linked list with function

  5. 5

    Segmentation fault when creating linked list with function

  6. 6

    Linked list function causing segmentation fault

  7. 7

    Why is this simple linked list program giving segmentation fault?

  8. 8

    segmentation fault linked list

  9. 9

    C - Node in a linked list somehow modified, results in segmentation fault

  10. 10

    Linked List in C - Segmentation Fault

  11. 11

    C segmentation fault linked list

  12. 12

    C segmentation fault linked list

  13. 13

    Linked list implementation segmentation fault

  14. 14

    Segmentation fault in linked list creation

  15. 15

    Segmentation Fault (Variant of Linked List)

  16. 16

    Segmentation fault in linked list C

  17. 17

    Pop Function In Linked List Stack Results in Segmentation Fault- C

  18. 18

    Segmentation fault - How to write and read a linked list from a pipe

  19. 19

    Insert a Node at Nth position in Doubly linked list using C

  20. 20

    Insert a Node at Nth position in Doubly linked list using C

  21. 21

    Basic Bool function giving segmentation fault

  22. 22

    Linked-list node delete function

  23. 23

    Segmentation fault when printing the linked list

  24. 24

    Segmentation Fault(Core Dump) Linked List

  25. 25

    Segmentation fault in singly linked list C program

  26. 26

    Segmentation fault when deleting linked list

  27. 27

    Segmentation fault while reading a file into a linked list

  28. 28

    Doubly Linked List insertion Segmentation Fault - C

  29. 29

    linked list segmentation fault fixed with cerr

HotTag

Archive