C 尝试使用循环设置哈希表中的新元素时失败
我正在尝试设置包含新元素的哈希表: 因此,我创建了一个数组C 尝试使用循环设置哈希表中的新元素时失败,c,C,我正在尝试设置包含新元素的哈希表: 因此,我创建了一个数组ligne[3][25]={NULL},并且: strcpy(ligne[0] , "mama"); strcpy(ligne[1] , "kaka"); strcpy(ligne[2] , "tata"); 使用ligne将元素添加到哈希表中时:有效:) 文本文件dico.txt包含: mama kaka tata 我添加元素的函数是ht\u集(哈希表,元素) * Insert a value into a hash table.
ligne[3][25]={NULL}
,并且:
strcpy(ligne[0] , "mama");
strcpy(ligne[1] , "kaka");
strcpy(ligne[2] , "tata");
使用ligne将元素添加到哈希表中时:有效:)
文本文件dico.txt包含:
mama
kaka
tata
我添加元素的函数是ht\u集(哈希表,元素)
* Insert a value into a hash table. */
entry_t* ht_set( hashtable_t *hashtable, char *key ) {
int bin = 0;
entry_t *newpair = NULL;
entry_t *next = NULL;
entry_t *last = NULL;
bin = ht_hash( hashtable, key );
next = hashtable->table[ bin ];
while( next != NULL && next->key != NULL ) {
last = next;
next = next->next;
}
/* There's already a pair. Let's replace that string. */
if( next != NULL && next->key != NULL) {
free( next->key );
next->key = strdup( key );
return next;
/* Nope, could't find it. Time to grow a pair. */
} else {
newpair = ht_newpair( key );
/* We're at the start of the linked list in this bin. */
if( next == hashtable->table[ bin ] ) {
newpair->next = next;
hashtable->table[ bin ] = newpair;
/* We're at the end of the linked list in this bin. */
} else if ( next == NULL ) {
last->next = newpair;
/* We're in the middle of the list. */
} else {
newpair->next = next;
last->next = newpair;
}
return newpair;
}
}
int main(int argc,char* argv []){
hashtable_t* hashtable = ht_create(62400);
int i=0;
char ligne[3][25]= {NULL}; // three char pointer that take each ligne
FILE* file = fopen("dico.txt", "r");
if(file != NULL){
while (fgets(ligne[i], 25, file) != NULL ){
i++;
}
for(i = 0 ; i < 3 ; i++){
ht_set(hashtable, ligne[i] );
}
printf("%s ", ht_get(hashtable , "mama"));// Search "mama" in the hashtable if exist then write it if not exist then printf (null)
}
free(hashtable);
fclose(file);
return 0;
}
我的代码是:
* Insert a value into a hash table. */
entry_t* ht_set( hashtable_t *hashtable, char *key ) {
int bin = 0;
entry_t *newpair = NULL;
entry_t *next = NULL;
entry_t *last = NULL;
bin = ht_hash( hashtable, key );
next = hashtable->table[ bin ];
while( next != NULL && next->key != NULL ) {
last = next;
next = next->next;
}
/* There's already a pair. Let's replace that string. */
if( next != NULL && next->key != NULL) {
free( next->key );
next->key = strdup( key );
return next;
/* Nope, could't find it. Time to grow a pair. */
} else {
newpair = ht_newpair( key );
/* We're at the start of the linked list in this bin. */
if( next == hashtable->table[ bin ] ) {
newpair->next = next;
hashtable->table[ bin ] = newpair;
/* We're at the end of the linked list in this bin. */
} else if ( next == NULL ) {
last->next = newpair;
/* We're in the middle of the list. */
} else {
newpair->next = next;
last->next = newpair;
}
return newpair;
}
}
int main(int argc,char* argv []){
hashtable_t* hashtable = ht_create(62400);
int i=0;
char ligne[3][25]= {NULL}; // three char pointer that take each ligne
FILE* file = fopen("dico.txt", "r");
if(file != NULL){
while (fgets(ligne[i], 25, file) != NULL ){
i++;
}
for(i = 0 ; i < 3 ; i++){
ht_set(hashtable, ligne[i] );
}
printf("%s ", ht_get(hashtable , "mama"));// Search "mama" in the hashtable if exist then write it if not exist then printf (null)
}
free(hashtable);
fclose(file);
return 0;
}
intmain(intargc,char*argv[]){
hashtable_t*hashtable=ht_create(62400);
int i=0;
char ligne[3][25]={NULL};//获取每个ligne的三个char指针
FILE*FILE=fopen(“dico.txt”、“r”);
如果(文件!=NULL){
while(fgets(ligne[i],25,file)!=NULL){
i++;
}
对于(i=0;i<3;i++){
ht_集(hashtable,ligne[i]);
}
printf(“%s”,ht_get(哈希表,“mama”);//如果存在,则在哈希表中搜索“mama”,如果不存在则写入,然后printf(null)
}
自由(哈希表);
fclose(文件);
返回0;
}
谢谢你的帮助,这花了我5天的时间,我真的无法解决这个问题。LOL LOL上面的代码似乎没有显示错误,或者从中看不出明显的错误。请发一张MCVE。请参阅:。“增加循环时读入另一个变量(如
cnt
)的字符串数,并在for
循环中使用它。看起来您在中错误地使用了i
,而在之后是for
循环,您在不知道发生了什么的情况下更改了某些内容。上面的代码似乎没有显示错误,或者从中看不明显。请发一张MCVE。请参阅:.“将while循环中读取的字符串数增加到另一个变量中,如cnt
,并在for
循环中使用它。看起来您在while
和for
循环中都错误地使用了i
,您在不知道是什么的情况下更改了某些内容。”。。
int main(int argc,char* argv []){
hashtable_t* hashtable = ht_create(62400);
int i=0;
char ligne[3][25]= {NULL}; // three char pointer that take each ligne
FILE* file = fopen("dico.txt", "r");
if(file != NULL){
while (fgets(ligne[i], 25, file) != NULL ){
i++;
}
for(i = 0 ; i < 3 ; i++){
ht_set(hashtable, ligne[i] );
}
printf("%s ", ht_get(hashtable , "mama"));// Search "mama" in the hashtable if exist then write it if not exist then printf (null)
}
free(hashtable);
fclose(file);
return 0;
}