Varun Raghav B
Varun Raghav B

Reputation: 73

How to overcome segmentation error while performing addition of 2 singly-linked lists?

In this code below ,i have got numerals as input,and send the odd numbers to list1 and sent the even numbers to list2.Finally,i have added the values in list1 & list2 and stored them in list3.But i am getting segmentation error.please help me

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

struct node//list creation
{
    int data;
    struct node *next;
};

struct node *list1;
struct node *list2;
struct node *list3;

/*creating list*/
void create(struct node *l, int x)
{
    l->data = x;
    l->next = NULL;
}

/* adding elements to list*/
void addlast(struct node *li, int x)
{
    struct node *temp = NULL;
    temp = (struct node *) malloc(sizeof(struct node));
    temp->data = x;
    while (li->next != NULL)
        li = li->next;
    li->next = temp;
    temp->next = NULL;
}

/* printing values */
void print(struct node *lb)
{
    if (lb == NULL)
        printf("empty");
    else
    {
        while (lb->next != NULL)
        {
            printf(" % d->", lb->data);
            lb = lb->next;
        }
        printf(" % d->", lb->data);
    }
}

/* performing addition */
void add(struct node *l1, struct node *l2)
{
    int value, c = 0;
    while (l1->next != NULL || l2->next != NULL)
    {
        value = l1->data+l2->data;
        if (c == 0)
        {
            create(list3, value);
            c++;
        }
        else
        {
            addlast(list3, value);
        }
        l1 = l1->next;
        l2 = l2->next;
    }
    printf("list3");
    print(list3);
}

int main()
{
    int i, n, a[20], c1 = 0, c2 = 0;
    list1 = (struct node *) malloc(sizeof(struct node));
    list2 = (struct node *) malloc(sizeof(struct node));
    list3 = (struct node *) malloc(sizeof(struct node));

    printf("\n Enter the number of numbers");
    scanf("%d", &n);
    for (i = 0; i < n; i++)
    {
        scanf("%d", &a[i]);

        if (a[i] % 2 == 0)
        {
            if (c1 == 0)
            {
                create(list1, a[i]);
                c1++;
            }
            else
                addlast(list1, a[i]);
        }
        if (a[i] % 2 != 0)
        {
            if (c2 == 0)
            {
                create(list2, a[i]);
                c2++;
            }
            else
                addlast(list2, a[i]);
        }

    }
    printf("list1");
    print(list1);
    printf("\n");
    printf("list2");
    print(list2);
    add(list1, list2);
    return 0;
}

Upvotes: 0

Views: 34

Answers (1)

knightrider
knightrider

Reputation: 2143

The problem is your while loop condition in the add. You should check if either l1->next or l2->next is null. Here is the corrected version.

/* performing addition */
void add(struct node *l1, struct node *l2)
{
    int value, c = 0;
    //you can only add if the two lists have same number of elems
    while (l1->next != NULL && l2->next != NULL)
    {
        value = l1->data + l2->data;
        if (c == 0)
        {
            create(list3, value);
            c++;
        }
        else
        {
            addlast(list3, value);
        }
        l1 = l1->next;
        l2 = l2->next;
    }

    //if lists dont have equal number of elements
    //find the list which is not empty and append the
    //elems to l3 here
    printf("list3");
    print(list3);
}

Upvotes: 3

Related Questions