作者:手机用户2502913853 | 来源:互联网 | 2023-05-18 16:08
Imtryingtomakeanarrayofnodes,andentervaluesfromS[]arraytoit.ButIkeepgettingaseg
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; jnext=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 个解决方案