1

Wrote this program in C for linked Lists. I am getting an error in insert when trying to insert at the end of the linked list as a segmentation fault. All other cases are working such as inserting initially,inserting in between 2 elements,etc. Spent a lot of time pondering but couldnt figure out?

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

void insert();
void display();
void search();
void delete();
struct node{
    int val;
    struct node *next;
}*head;
int num,count=0;

void main()
{
    char dec = 'E';
    printf("Welcome to the Linked List C Program!\n");
    head = NULL;

    while(1){
        printf("Make a Choice:\n");
        printf("I.Insert\nD.Delete\nS.Search\nd.Display\nE.Exit\n");
        scanf(" %c",&dec);
        switch(dec){
            case 'I':
                insert();
                break;
            case 'D':
                delete();
                break;
            case 'S':
                search();
                break;
            case 'd':
                display();
                break;
            case 'E':
                exit(0);
                break;
            default:
                printf("Wrong input Try Again!\n");
        }
    }
}

void insert(){
    printf("Enter the number to insert:");
    scanf("%d",&num);
    struct node *temp;
    struct node *newnode;
    struct node *prev;
    int c =0;
    //temp = (struct node *)malloc(sizeof(struct node));
    newnode = (struct node *)malloc(sizeof(struct node));
    //prev = (struct node *)malloc(sizeof(struct node));
    newnode->val = num;
    if(head==NULL)
    {
        head = newnode;
        head->next = NULL;
    }
    else
    {
        temp = head;
        //searching whether linked list is in start or end
        while(temp->val<num && temp !=NULL)
        {
            printf("Index:%d Value:%d Address:%p",c,temp->val,temp);
            prev = temp;
            temp = temp->next;
            c++;
            printf("TEST\n");

        }
        if(c==0)
        {
            head = newnode;
            head->next = temp;

        }
        else
        {
            prev->next=newnode;
            newnode->next=temp;
        }
    }
}

void display()
{
    struct node *temp;
    temp = (struct node *)malloc(sizeof(struct node));
    temp = head;
    while(temp != NULL)
    {
        printf("%d",temp->val);
        temp = temp->next;
    }
    if(temp == NULL)
    {
        printf("Not present!");
    }

}

void search()
{
    printf("Enter the number tosearch for:\n");
    scanf("%d",&num);
    struct node *temp;
    temp=head;
    int c=0;
    while(temp!=NULL)
    {
        if(temp->val==num)
        {
            printf("Present at position %d",c);
            c++;
        }
        else if(temp == NULL)
        {
            printf("Not present!");
        }
        else
            c++;
        temp =temp->next;
    }
}

void delete()
{
    struct node *temp;
    struct node *prev;
    temp = head;
    printf("Enter the value to delete:\n");
    scanf("%d",&num);
    int c=0;
    while(temp!=NULL)
    {
        if(temp->val==num)
        {
            printf("Present at position %d",c);
            printf("Deleting this element");
            prev->next=temp->next;
            free(temp);
            c++;
        }
        else
            c++;
        prev = temp;
        temp =temp->next;

    }
    if(temp == NULL)
    {
        printf("Not present!");
    }

}
1
  • 1
    Compile with all warnings and debugging info (e.g. gcc -Wall -g) and use the debugger (e.g. gdb) Commented Aug 29, 2013 at 19:20

1 Answer 1

6

In your while loop, you're checking one of temp's members before checking that temp is not NULL:

while(temp->val<num && temp !=NULL)

reverse that:

while ((temp != NULL) && (temp->val < num))
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks! Working perfectly

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.