Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
167 views
in Technique[技术] by (71.8m points)

algorithm - Segmentation Default

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("
Enter choice
");
        printf("1.Append
");
        printf("2.In between
");
        printf("3.At the beginning
");
        printf("4.Display
");
        printf("5.Delete
");
        printf("6.Delete from between
");
        printf("7.Quit
");
        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.

question from:https://stackoverflow.com/questions/65882918/segmentation-default

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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;

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...