c - 如何在执行 2 个单链表的添加时克服段错误?

标签 c pointers data-structures linked-list

在下面的代码中,我将数字作为输入,并将奇数发送到 list1 并将偶数发送到 list2。最后,我将值添加到 list1 和 list2 中并将它们存储在 list3 中。但我是出现段错误。请帮助我

#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;
}

最佳答案

问题是您在添加中的 while 循环条件。您应该检查 l1->next 或 l2->next 是否为空。这是更正后的版本。

/* 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);
}

关于c - 如何在执行 2 个单链表的添加时克服段错误?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33184300/

相关文章:

ios - HQ2x 图像放大会产生损坏的 PNG

c - typedef void* key_type 在 C 中是什么意思?

database - Golang worker 池导致数据库损坏

algorithm - 即使只有一个负权重边,Dijkstra 算法是否适用?

c - 无需条件语句访问第 n 位

c - 如何打印乘数 3 和 5,但不打印乘数 15?

python - 当我们循环遍历所有元素时保留前 N 个元素

python - 如何在 Python 中存储键值和值键?

c - 在 FPU 和 SSE 发明之前, float 转换是如何处理的?

c++ - 堆指针分配之间的差异