0

I'm trying to create a simple linked list and insert a node at the end of the list. I'm getting a segmentation fault.

#include <stdio.h>
#include <stdlib.h>

struct node{
    int data;
    struct node *link;
};

void create(struct node *head){
    struct node* second = NULL;
    struct node* last = NULL;

    second = (struct node*)malloc(sizeof(struct node));
    last = (struct node*)malloc(sizeof(struct node));

    second -> data = 2;
    last -> data = 3;

    head -> link = second;
    second -> link = last;
    last -> link = NULL;
}

void insert_ending(struct node *head){
    struct node* temp = NULL;
    struct node* temp1 = NULL;

    temp1 = (struct node*)malloc(sizeof(struct node));
    temp1 -> data = 5;
    temp1 -> link = NULL;

    temp = head;
    while(temp != NULL){
       temp = temp -> link;
    }temp -> link = temp1;
}

void PrintList(struct node *head){
    while( head != NULL ){
        printf(" %d -->", head -> data);
        head = head -> link;
    }
    printf("\n");
}

int main(){
    struct node* head = NULL;
    head = (struct node*)malloc(sizeof(struct node));
    head -> data = 1;
    head -> link = NULL;

    create(head);
    PrintList(head);

    insert_ending(head);
    PrintList(head);
    return 0;
}

I'm getting a segmentation fault. The output is as follows.

1 --> 2 --> 3 --> Segmentation fault (core dumped)

2
  • Dont you think you are modifying the head pointer? Commented May 17, 2015 at 12:47
  • if you use a sentry node all special cases regarding first node, last node, empty list etc go away Commented May 17, 2015 at 12:54

2 Answers 2

3

in your insert function you need to change to :

 temp = head;
    while(temp -> link != NULL){
       temp = temp -> link;
    }
    temp -> link = temp1;

the reason is that when you loop with while until temp == null, you can't afterward do: temp -> link because temp is allready null.

Sign up to request clarification or add additional context in comments.

4 Comments

When do I use (temp -> link != NULL) and when to use ( temp != NULL)
you are using it in the insert_ending function, instead of the for while loop there.
Not in this example, in a general case, where do I use it?
There is no rule for that, you need to pay attention when you need to itterate until the last node of the list (of Tree and etc.) or to itterate to the one before. If you are new in programming, I suggest that any time you have to deal with such situations, paint the scenario on some paper and you'll see it by yourself (It worked for me :-) )
3

In the function 'insert_ending' in the while loop you want to change the condition from:

while ( temp != NULL ) 

to:

while ( temp->link != NULL )

because now once the loop is finished, temp is NULL and then you try to dereference it (a NULL pointer) and get an error.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.