Home > Net >  Segmentation fault in linked list program (c language)
Segmentation fault in linked list program (c language)

Time:11-12

I'm trying to write a code for linked lists using c . Insert at begin and Insert at end are not working for some reason. Here is the code.

`

#include<stdio.h>
#include<stdlib.h>
#include<conio.h>
void insertAtBeginning(int );
void insertAtEnd(int );
void printLL();

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


int main()
{
    struct Node *temp, *newnode;
    int ch=1, i=1, info;
    head = NULL;
    while(ch)
    {
        printf("Enter data: ");
        scanf("%d", &info);
        
        newnode = (struct Node *)malloc(sizeof(struct Node));
        
        newnode->data = info;
        newnode->link = NULL;
        
        if(head == NULL)
        {
            head = newnode;
            temp = newnode;
        }
        else
        {
            temp ->link = newnode;
            temp = newnode;
        }
        printf("You wish to continue? (press 0 to terminate)\n");
        scanf("%d",&ch);
        if(!ch)
        {
            break;
        }
    }
    temp = head;
    while(temp!=NULL)
    {
        printf("%d -> ",temp->data);
        temp = temp->link;
    }
    printf("\n");
    insertAtBeginning(50);
    insertAtEnd(150);
    //printLL();
    
}

void insertAtBeginning(int info)
{
    struct Node *newnode;
    newnode->data = info;
    printf("\n%d\n", newnode ->data);
    newnode->link = head;
    head = newnode;
}

void insertAtEnd(int info)
{
    struct Node *temp, *newnode;
    newnode->link = NULL;
    newnode->data = info;
    temp = head;
    while(temp!=NULL)
    {
        temp = temp->link;
    }
    temp->link = newnode;
    printf("\n%d\n", newnode -> data);
}

void printLL()
{
    struct Node *temp;
    temp = head;
    while(temp!=NULL)
    {
        printf("%d -> ",temp->data);
        temp = temp->link;
    }
}

`

The problem is somewhere around newnode->data = info in the functions.

I created two functions, one to insert an element at beginning and one to insert an element at end. In both of them, i've created a newnode. The problem is I cannot insert data into those nodes.

CodePudding user response:

When you want to append a new node to the end of a linked list, you must find your last node (a node which its link is NULL, not itself). Also, it is better to use meaningful variable name (temp is too general name). You also forgot to malloc new nodes in insertAtBeginning and insertAtEnd functions. I've fixed these issues in the following code

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

void insertAtBeginning(int );
void insertAtEnd(int );
void printLL();

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

struct Node *head = NULL;


int main()
{
    struct Node *it, *newnode, *tail;
    int ch=1, i=1, info;

    while(ch){
        printf("Enter data: ");
        scanf("%d", &info);

        newnode = malloc(sizeof(struct Node));
        newnode->data = info;
        newnode->link = NULL;

        if (head == NULL) {
            head = newnode;
            tail = newnode;
        } else {
            tail->link = newnode;
            tail = newnode;
        }

        printf("You wish to continue? (press 0 to terminate, else to continue)\n");
        scanf("%d",&ch);
        if(ch == 0) {
            break;
        }
    }

    it = head;
    while(it != NULL) {
        printf("%d -> ",it->data);
        it = it->link;
    }

    printf("\n");
    insertAtBeginning(50);
    insertAtEnd(150);
}

void insertAtBeginning(int info)
{
    struct Node *newnode;

    newnode = malloc(sizeof(struct Node));
    newnode->data = info;
    printf("\n%d\n", newnode->data);
    newnode->link = head;
    head = newnode;
}

void insertAtEnd(int info)
{
    struct Node *it, *newnode;

    newnode = malloc(sizeof(struct Node));
    newnode->link = NULL;
    newnode->data = info;
    it = head;
    while(it->link != NULL)
    {
        it = it->link;
    }
    it->link = newnode;
    printf("\n%d\n", newnode->data);
}

void printLL()
{
    struct Node *it;
    it = head;
    while(it!=NULL)
    {
        printf("%d -> ",it->data);
        it = it->link;
    }
}

CodePudding user response:

You're treating your uninitialized stack variable (struct Node *newnode) as a pointer to a struct Node and trying to update its fields:

struct Node *newnode;
newnode->data = info;

This doesn't work because the value of newnode is whatever garbage was on the stack beforehand, and trying to deref it (*newnode) will likely give a seg fault since you're trying to read from some unknown memory address.

Notice how inside main, you assign a result from malloc to newnode, this means you know that (given that malloc didn't return NULL), the pointer is valid, and you're free to use the memory it points to.

  • Related