1

I trying to write basic Linked list code. I have two functions to add data to beginning of list and end of the list. Function to add data in the beginning works fine every time.

I am facing segmentation fault in insert_end function always. I am getting the error while trying to access temp1 pointer to structure after doing malloc to it. But this is the same exact thing I am doing even in the insert_first function, but works every time. I tried googling it and tried in forums, no answer. Please help.

This link is my exact kinda problem.. but i dont understand the solution fully

Segmentation fault in C with malloc

I am getting error particularly on this block

struct node *temp1,*trav;
    temp1 = (struct node *)malloc(sizeof(struct node));
    trav = (struct node *)malloc(sizeof(struct node));
    if (temp1 != NULL) { //******************Segmentation fault at this line**********
        temp1->data = input;
    }






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

//*******Structure to hold linked list*********//
struct node {
    int data;
    struct node *next;
}*head,*temp;


//***********Function to Display everything**********//

void display(struct node *head) {
    struct node *trav;
    trav= (struct node *)malloc(sizeof(struct node));
    printf("Entering into Display\n");
    if (head == NULL) {
        printf("Oh My God, the list is empty\n");
    }
    else {
        trav = head;
        while (trav != NULL) {
            printf("Value stored in [%p] is [%d]\n",trav,trav->data);
            trav = trav->next;
        }
    }
}


//***********Function to Insert at beginning*********//

struct node *insert_first(struct node *head,int input) {
    temp = (struct node *)malloc(sizeof(struct node));
    temp->data = input;
    printf("\nEntering insert first");
    if (head == NULL) {
        head = temp;
        head->next = NULL;
    }
    else {
        temp->next = head;
        head = temp;
    }
    return head;
}


//**************Function to Insert at End******************//
struct node *insert_last(struct node *head,int input) {
    struct node *temp1,*trav;
    temp1 = (struct node *)malloc(sizeof(struct node));
    trav = (struct node *)malloc(sizeof(struct node));
    if (temp1 != NULL) {
        temp1->data = input;
    }
    else {
        printf("empty");
    }
    printf("\nEntering insert last");
    if (head == NULL) {
        head = temp1;
        head->next = NULL;
    }
    else {
        trav = head;
        while (trav != NULL) {
            trav = trav->next;
        }
        trav->next = temp1;
    }
    return head;
}




//*************Main Fucntion***********//

int main() {
    int choice,value;
    head = NULL;
    while(1) {
        printf("\n******Please Enter your choice****\n1. To insert at beginning\n2. To insert at End\n3. To Insert middle\n4. To delete\n5. To display\n0. To Exit\n");
        scanf("%d",&choice);
        switch(choice){
            case 1:
                printf("Please Enter the value to be added\n");
                scanf("%d",&value);
                head = insert_first(head,value);
                break;
            case 2:
                printf("Please Enter the value to be added\n");
                scanf("%d",&value);
                head = insert_last(head,value);
                break;

            case 5:
                display(head);
                break;
            case 0:
                return 0;
            default:
                printf("Thats a wrong choice\n");
                break;
        }
    }
}
4
  • What insert_end do you meen? Commented Apr 26, 2014 at 6:06
  • There are so many problems with that code: memleak:display() "malloc trav" without free or any assignment, memleak:insert_last() "malloc trav" without free or any assignment. Commented Apr 26, 2014 at 6:10
  • Sorry!!1 insert_last, in fact Commented Apr 26, 2014 at 6:15
  • Yes @harper. I understand there are lot of problems, I am beginner trying to learn C now. Will correct it as and when required. But I am blocked right now. Please help Commented Apr 26, 2014 at 6:40

1 Answer 1

3

This is the problematic block.

else {
    trav = head;
    while (trav != NULL) {
        trav = trav->next;
    }
    trav->next = temp1;

When you come out of the while loop trav is NULL. That makes the line

    trav->next = temp1;

fail with segmentation violation.

Change that block to:

else {
    trav = head;
    while (trav->next != NULL) {
        trav = trav->next;
    }
    trav->next = temp1;
Sign up to request clarification or add additional context in comments.

2 Comments

Also needs to set temp1->next and should not allocate trav at all.
@Sahu in my case, the segmentation fault occurs much before reaching this particular block. I am getting error while accessing temp1 after malloc ing it!!!

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.