0

Creation of node:

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

struct Node *head = NULL;

Append Function

int append()
{
    struct Node *temp;
    struct Node *p;
    temp = (struct Node *)malloc(sizeof(struct Node));
    printf("Enter the data");
    scanf("%d", &temp->data);
    
    if (head == NULL)
    {   temp->link = NULL;
        head = temp;
        
    }
    else
    {
         
        p = head;
        while (p != NULL)
        {
            p = p->link;
        }
     p->link=NULL;
    }
    return p->data;
}

Main Function

void main()
{
    int append();
    int insert();
    int insert_begin();
    int display();
    int delete ();
    int del_between();
    int k = 1, ch ,d;
    while (k)
    {
        printf("\nEnter choice\n");
        printf("1.Append\n");
        printf("2.In between\n");
        printf("3.At the beginning\n");
        printf("4.Display\n");
        printf("5.Delete\n");
        printf("6.Delete from between\n");
        printf("7.Quit\n");
        scanf("%d", &ch);
        switch (ch)
        {
        case 1:
            d = append();
            printf("pushed %d",d);
            break;
        case 2:
            insert();
            break;
        case 3:
            insert_begin();
            break;
        case 4:
            display();
            break;
        case 5:
            delete ();
            break;
        case 6:
            del_between();
            break;
        case 7:
            k = 0;
            break;
        default:
            printf("wrong choice");
        }
    }
}

I have been trying to append a node at the end of a linked list but as soon as I enter the data to be added Segmentation default error occurs.

Output screen

Enter choice
1.Append
2.In between
3.At the beginning
4.Display
5.Delete
6.Delete from between
7.Quit
1
Enter the data23
Segmentation fault

.........................................................................................................

What is the meaning of Segmentation fault ?How to get rid of it? Where am I going wrong? ................................................................................................................

Thanks.

  • 2
    When describing a problem, it is essential to report the error messages accurately. There is nothing like a "segmentation default". –  Jan 25 '21 at 10:39
  • See hyperlink under "Related". And [List of Reasons](https://stackoverflow.com/questions/33047452/definitive-list-of-common-reasons-for-segmentation-faults). – greybeard Jan 25 '21 at 11:35

1 Answers1

-1

Segmentation fault occurs when you try to access some memory which is restricted.

The error came because in your case linked list is empty and first time when you call append function it goes to your if statement, and in your if head = temp; so you are updating your head with temp data. Further when the if condition ends you are returning the value of p return p->data; but this wont work.

As p is only initialized but with no data value thus accessing something which is not assigned giving SIGSEGV.

you can resolve the error by editing :

if (head == NULL)
{   temp->link = NULL;
    head = temp;
    return head->data;
}
else
{
     
    p = head;
    while (p != NULL)
    {
        p = p->link;
    }
 p->link=temp;
 p=temp;
 temp->link=NULL;
}
return p->data;
  • I am beginner in data structure, can you help me out with the required changes in the code? – Tanya Gupta Jan 25 '21 at 13:52
  • yes, just include `return head->data;` in your if statement inside your append function and your code will work fine. – Vibhav Sharma Jan 25 '21 at 14:13
  • @TanyaGupta I have also updated the ans. with code edits wherever required, finally your append function will now return the int value of the data that was just appended in the end of your linked list. – Vibhav Sharma Jan 25 '21 at 14:39
  • How do you form an opinion whether your suggestions lead to effective code? `you can resolve the error [as below]` Nope. *include `return head->data;`* wrong, again. – greybeard Jan 25 '21 at 16:47
  • @greybeard I accept that the code is not effective but it resolves the segmentation fault error and I just provided a soln so that user can have a way with it to work. – Vibhav Sharma Jan 25 '21 at 17:23
  • I understand that for optimization we could have get both head and tail of a linked list so that insertion will take only O(1) time. But for a beginner i guess it wont be easy to understand optimization until and unless they try the bruteforce soln first. – Vibhav Sharma Jan 25 '21 at 17:27