插入到c中的链表时的错误值
Wrong value when inserting into a linked list in c
我正在尝试将单词插入散列 table 并且它看起来有效但是当我尝试在节点内打印单词时(只是为了检查它是否仍然正确)我得到了一个虚假值.当我的代码提示输入单词时,我说 'Hey',当它提示输入位置时,我说 '5'。打印出来的字符串(应该是节点里面的单词)是 HH9[]A\A]A^A_f. 发生了什么节点中的单词,我是否正确插入了节点?
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
typedef struct node
{
char word[20];
struct node *next;
}
node;
int main (void)
{
node* table[10];
char wrd[10];
printf("Word to insert: ");
fgets(wrd, 10, stdin);
int place;
printf("Place to insert word: ");
scanf("%d", &place);
node *n = malloc(sizeof(node));
if(n == NULL)
{
return 1;
}
strcpy(n->word, wrd);
if(table[place] == NULL)
{
n = table[place];
n->next = NULL;
}
else
{
n->next = table[place];
n = table[place];
}
printf("Word inside node: %s \n" , n->word);
}
编辑
我更改了代码并尝试在更大范围内实施它,但我的 while 循环给了我一个段错误。这是我放入的函数:
FILE* dct = fopen ("/dictionaries/large", "r");
char *wrd = NULL;
while(fscanf(dct, "%s", wrd) != EOF)
{
int place = hash(wrd);
node *n = malloc(sizeof(node));
node *anchor = NULL;
node *end = NULL;
if(n == NULL)
{
return 1;
}
strcpy(n->word, wrd);
n->next = NULL;
if (!end) //Initial state
anchor = end = n;
else //Every following node.
end = end->next = n;
strcpy(n->word, wrd);
n->next = table[place];
table[place] = n;
counter++;
}
return false;
它必须从字典文件中读取并将单词加载到内存中(或散列 table)。
链表之所以是链表,是因为它没有固定的大小。
因此 table
数组是多余的。
链表工作所需的只是记住锚点,仅此而已。
一个小例子:
Node *anchor = NULL;
Node *end = NULL;
Node *node = malloc(sizeof(Node));
node->next = NULL;
if (!end) //Initial state
anchor = end = node;
else //Every following node.
end = end->next = node;
此时,您仍然可以访问刚刚填写的node
。不要忘记稍后迭代您的列表和 free
这些分配。
这段代码没有任何意义:
if(table[place] == NULL)
{
n = table[place]; // since we know table[place] is null, that sets n to null!
n->next = NULL; // We just set n to NULL, we can't access n->next!
}
else
{
n->next = table[place]; // This sets n to a garbage value since table[place] was never assigned a value
n = table[place]; // This leaks the value we malloc'ed. We were supposed to link it to the list!
}
我正在尝试将单词插入散列 table 并且它看起来有效但是当我尝试在节点内打印单词时(只是为了检查它是否仍然正确)我得到了一个虚假值.当我的代码提示输入单词时,我说 'Hey',当它提示输入位置时,我说 '5'。打印出来的字符串(应该是节点里面的单词)是 HH9[]A\A]A^A_f. 发生了什么节点中的单词,我是否正确插入了节点?
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
typedef struct node
{
char word[20];
struct node *next;
}
node;
int main (void)
{
node* table[10];
char wrd[10];
printf("Word to insert: ");
fgets(wrd, 10, stdin);
int place;
printf("Place to insert word: ");
scanf("%d", &place);
node *n = malloc(sizeof(node));
if(n == NULL)
{
return 1;
}
strcpy(n->word, wrd);
if(table[place] == NULL)
{
n = table[place];
n->next = NULL;
}
else
{
n->next = table[place];
n = table[place];
}
printf("Word inside node: %s \n" , n->word);
}
编辑
我更改了代码并尝试在更大范围内实施它,但我的 while 循环给了我一个段错误。这是我放入的函数:
FILE* dct = fopen ("/dictionaries/large", "r");
char *wrd = NULL;
while(fscanf(dct, "%s", wrd) != EOF)
{
int place = hash(wrd);
node *n = malloc(sizeof(node));
node *anchor = NULL;
node *end = NULL;
if(n == NULL)
{
return 1;
}
strcpy(n->word, wrd);
n->next = NULL;
if (!end) //Initial state
anchor = end = n;
else //Every following node.
end = end->next = n;
strcpy(n->word, wrd);
n->next = table[place];
table[place] = n;
counter++;
}
return false;
它必须从字典文件中读取并将单词加载到内存中(或散列 table)。
链表之所以是链表,是因为它没有固定的大小。
因此 table
数组是多余的。
链表工作所需的只是记住锚点,仅此而已。
一个小例子:
Node *anchor = NULL;
Node *end = NULL;
Node *node = malloc(sizeof(Node));
node->next = NULL;
if (!end) //Initial state
anchor = end = node;
else //Every following node.
end = end->next = node;
此时,您仍然可以访问刚刚填写的node
。不要忘记稍后迭代您的列表和 free
这些分配。
这段代码没有任何意义:
if(table[place] == NULL)
{
n = table[place]; // since we know table[place] is null, that sets n to null!
n->next = NULL; // We just set n to NULL, we can't access n->next!
}
else
{
n->next = table[place]; // This sets n to a garbage value since table[place] was never assigned a value
n = table[place]; // This leaks the value we malloc'ed. We were supposed to link it to the list!
}