【发布时间】:2019-02-18 02:22:13
【问题描述】:
我正在尝试在节点数组中创建一个链表。当我尝试将 arrTab->h_table[index] 的指针更新为 newNode 的地址时,该地址指向 newNodes 地址。但是当我尝试添加到数组中存在的列表时,指针总是指向 NULL 而不是内存中的前一个值。基本上链表的 arrTab->h_table[index] 头部不会更新到 newNode 的地址。
typedef struct node {
struct node* next;
int hash;
s_type symbol;
} node_t;
struct array {
int cap;
int size;
n_type** h_table;
};
int add_to_array (array* arrTab, const char* name, int address) {
if(s_search(arrTab, name, NULL, NULL) == NULL){
s_type *symbol = (s_type*) calloc(1, sizeof(s_type));
symbol->name = strdup(name);
symbol->addr = addr;
n_type *newNode = (n_type*) calloc(1, sizeof(n_type));
newNode->next = NULL;
newNode->hash = nameHash(name);
newNode->symbol = *symbol;
int index = newNode->hash % arrTab->cap;
if(arrTab->h_table[index] == NULL){
arrTab->h_table[index] = newNode;
} else {
newNode->next = arrTab->h_table[index];
arrTab->h_table[index] = newNode;
}
//
arrTab->size++;
return 1;
}
return 0;
}
struct node* s_search (array* arrTab, const char* name, int* hash, int* index) {
int hashVal = nameHash(name);
hash = &hashVal;
int indexVal = *hash % arrTab->cap;
index = &indexVal;
s_type *symCopy = arrTab;
while (symCopy->h_table[*index] != NULL){
if(*hash == symCopy->h_table[*index]->hash){
return symCopy->h_table[*index];
}
symCopy->h_table[*index] = symCopy->h_table[*index]->next;
}
return NULL;
}
【问题讨论】:
-
版主注意:请不要破坏您的帖子。一旦您发布问题,它们就属于该网站及其用户。即使它不再对您有用,它也可能对将来的某人有所帮助。回答者也会努力写下他们的答案,如果您从帖子中删除了内容,这将不再有用。另外,请注意,通过在 Stack Exchange 网络上发布,您已授予 SE 分发该内容的不可撤销的权利(根据 CC BY-SA 3.0 许可)。根据 SE 政策,任何破坏行为都将被撤销。
标签: c