链接列表数组在C中无法正常工作

时间:2021-12-27 21:28:26

I'm trying to make an array of nodes,and enter values from S[] array to it. But I keep getting a segmentation fault. My struct looks like this:

我正在尝试创建一个节点数组,并从S []数组中输入值。但我一直遇到分段错误。我的结构看起来像这样:

typedef struct Node {
int num;
struct Node *next;
}ListNode;

Initialized array of S[10] with random numbers:

带有随机数的S [10]的初始化数组:

    printf("list is:");

    for(i=0;i<10; i++)
    {
            RN= (random()+1);
            S[i]=RN;
            printf("%d  ",S[i]);
    }
    printf("\n");

Here is how I initialized my array of nodes:

以下是我初始化节点数组的方法:

    ListNode *bucket[Radix];
    for(j=0; j<Radix; j++)
      {
            bucket[i]=(ListNode*)malloc(sizeof(ListNode));
            bucket[i]->next=NULL;
      }

And this is the function I use to read array numbers from S[] into the array of linked list, bucket[]:

这是我用来将S []中的数组编号读入链表,bucket []数组的函数:

     for(y=0;y<(sizeof(S)/sizeof(int));y++) // S is size of a normal array
        {
            digit=bucketNumber(S[y],1);// returns the first digit values
            pointer= bucket[digit];
            number= S[y];
            insert_tail(pointer, number);
        }

my insert_tail function look like this:

我的insert_tail函数看起来像这样:

    ListNode *insert_tail(ListNode *tail, int data)
    // insert and element at tail

    {

    if (tail==NULL)
    { tail=(ListNode *)malloc(sizeof(ListNode));}

    else
    {
        while(tail->next !=NULL)

        tail->next = (ListNode *)malloc(sizeof(ListNode));
        tail= tail->next;
    }

    tail->num= data;
    tail->next=NULL;
    }

this is the bucketNumber function:

这是bucketNumber函数:

    int bucketNumber(int num,int digit)
    {
    int x, y;
    y= 10*digit;
    x= num%y;

    if(digit>=2)
    {
      num= num%y;
      x= num/(y/10);
    }
    return (x);

}

}

I think the reason for segmentation fault is that my function is not creating the links in the array properly. I'm not sure thou, there could be something else wrong!

我认为分段错误的原因是我的功能是没有正确地在数组中创建链接。我不确定你,可能还有别的错!

1 个解决方案

#1


2  

I think the problem is in this section of insert_tail():

我认为问题出在insert_tail()的这一部分:

else
    {
        while(tail->next !=NULL)

        tail->next = (ListNode *)malloc(sizeof(ListNode));
        tail= tail->next;
    }

    tail->num= data;
    tail->next=NULL;

Since the while() loop doesn't have curly braces following it, the below line gets executed if and only if tail->next != NULL:

由于while()循环后面没有花括号,当且仅当tail-> next!= NULL时,才会执行以下行:

tail->next = (ListNode *)malloc(sizeof(ListNode));

...which is sort of the opposite of what you want; you want to allocate a new node for next if next is NULL. As is, next is likely NULL, and so tail gets moved forward to next, but next is not allocated--it's NULL. The bottom two lines above would each cause a segmentation fault in that case, since you can't dereference a NULL pointer, which is what -> is doing.

......这与你想要的相反;如果next为NULL,则要为next分配新节点。原样,next可能是NULL,因此tail会向前移动到next,但是next不会被分配 - 它是NULL。在这种情况下,上面的两行会导致分段错误,因为你不能取消引用NULL指针,这就是 - >正在做的事情。

#1


2  

I think the problem is in this section of insert_tail():

我认为问题出在insert_tail()的这一部分:

else
    {
        while(tail->next !=NULL)

        tail->next = (ListNode *)malloc(sizeof(ListNode));
        tail= tail->next;
    }

    tail->num= data;
    tail->next=NULL;

Since the while() loop doesn't have curly braces following it, the below line gets executed if and only if tail->next != NULL:

由于while()循环后面没有花括号,当且仅当tail-> next!= NULL时,才会执行以下行:

tail->next = (ListNode *)malloc(sizeof(ListNode));

...which is sort of the opposite of what you want; you want to allocate a new node for next if next is NULL. As is, next is likely NULL, and so tail gets moved forward to next, but next is not allocated--it's NULL. The bottom two lines above would each cause a segmentation fault in that case, since you can't dereference a NULL pointer, which is what -> is doing.

......这与你想要的相反;如果next为NULL,则要为next分配新节点。原样,next可能是NULL,因此tail会向前移动到next,但是next不会被分配 - 它是NULL。在这种情况下,上面的两行会导致分段错误,因为你不能取消引用NULL指针,这就是 - >正在做的事情。