HashTable文件导入单词无法检索问题求助
问题
手动向哈希表插入单词后可正常检索,但使用readwords函数从每行一个单词、包含约59000个单词的TXT文件导入单词后,仅能检索到最后一个单词ZULUS,其余单词均无法检索,调整代码后问题仍未解决。相关代码实现如下:
readwords函数代码
void readwords(HashTable H,char *filename){ char word[MAX_WORD_LENGTH]; char *line = NULL; size_t lineLength = 0; FILE* file = fopen(filename, "r"); if (file == NULL) { printf("Failed to open the file.\n"); return; } // 从文件读取单词并插入哈希表 while (getline(&line, &lineLength, file) != -1) { // 移除前导空格 char *trimmedLine = line + strspn(line, " \t"); // 从行中提取单词 char *token = strtok(trimmedLine, " \t\n"); while (token != NULL) { strcpy(word,token); // 复制无前置空格的单词 Insert(word, H); // 获取下一个单词 token = strtok(NULL, " \t\n"); } } fclose(file); }
hash.c代码
#include <stdio.h> #include <stdlib.h> #include <string.h> #include <stdbool.h> #include "hash.h" #define Error(Str) FatalError(Str) #define FatalError(Str) fprintf(stderr, "%s\n", Str), exit(1) #define MinTableSize (10) enum KindOfEntry { Legitimate, Empty, Deleted }; typedef char* ElementType; // 假设ElementType是字符串 struct HashEntry { ElementType Element; enum KindOfEntry Info; }; typedef struct HashEntry Cell; /* Cell *TheCells 是后续会分配的HashEntry数组 */ struct HashTbl { int Occupados; int TableSize; Cell* TheCells; }; bool IsPrime(int num) { if (num <= 1) return false; for (int i = 2; i * i <= num; i++) { if (num % i == 0) return false; } return true; } static int NextPrime(int N) { if (N <= 1) return 2; int prime = N; bool found = false; while (!found) { prime++; if (IsPrime(prime)) found = true; } return prime; } Index Hash( const char *Key, int TableSize ){ unsigned int HashVal = 0; while( *Key != '\0' ) HashVal = ( HashVal << 5 ) + *Key++; return HashVal%TableSize; } HashTable InitializeTable(int TableSize) { HashTable H; int i; if (TableSize < MinTableSize) { Error("Table size too small"); return NULL; } /* 分配表内存 */ H = malloc(sizeof(struct HashTbl)); if (H == NULL) FatalError("Out of space!!!"); H->TableSize = TableSize; /* 分配Cell数组内存 */ H->TheCells = malloc(sizeof(Cell) * H->TableSize); if (H->TheCells == NULL) FatalError("Out of space!!!"); for (i = 0; i < H->TableSize; i++) H->TheCells[i].Info = Empty; return H; } Position Find( ElementType Key, HashTable H ){ Position CurrentPos; int CollisionNum; CollisionNum = 0; CurrentPos = Hash( Key, H->TableSize ); while( H->TheCells[ CurrentPos ].Info != Empty && strcmp(H->TheCells[CurrentPos].Element, Key) != 0){ CurrentPos += 2 * ++CollisionNum - 1; if( CurrentPos >= H->TableSize ) CurrentPos -= H->TableSize; } return CurrentPos; } HashTable Rehash(HashTable H) { int i, OldSize; Cell* OldCells; OldCells = H->TheCells; OldSize = H->TableSize; /* 创建新的空表 */ H = InitializeTable(2 * OldSize); /* 遍历旧表,重新插入到新表 */ for (i = 0; i < OldSize; i++) if (OldCells[i].Info == Legitimate) Insert(OldCells[i].Element, H); free(OldCells); return H; } void Insert(ElementType Key, HashTable H) { Position Pos; Pos = Find( Key, H ); if( H->TheCells[ Pos ].Info != Legitimate ){ H->TheCells[ Pos ].Info = Legitimate; H->TheCells[Pos].Element = strdup(Key); H->Occupados++; } } float LoadFactor(HashTable H) { return (float)H->Occupados / H->TableSize; } ElementType Retrieve(Position P, HashTable H) { return H->TheCells[P].Element; } void DestroyTable(HashTable H) { free(H->TheCells); free(H); }
hash.h代码
#include <stdbool.h> #ifndef _Hash_H #define _Hash_H typedef char* ElementType; // 假设ElementType是字符串 typedef struct HashTbl *HashTable; typedef unsigned int Index; typedef Index Position; HashTable InitializeTable(int TableSize); void DestroyTable(HashTable H); Position Find(ElementType Key, HashTable H); void Insert(ElementType Key, HashTable H); float LoadFactor(HashTable H); HashTable Rehash(HashTable H); ElementType Retrieve(Position P, HashTable H); #endif // HASH_H /* 省略了Delete、MakeEmpty等函数 */ /* END */
main函数代码
int main(int argc, char const *argv[]){ char filename[] = "corncob_caps_2023.txt"; char filename2[] = "boggle0.txt"; int tableSize = 60000; HashTable words = InitializeTable(tableSize); readwords(words,filename); printf("%s",Retrieve(Find("ZULUS",words),words)); return 0; }
输入文件示例
AARDVARK AARDWOLF AARON ABACK ABACUS ABAFT ABALONE ABANDON ABANDONED ABANDONMENT
内容的提问来源于stack exchange,提问作者L1nK
相关产品推荐
相关产品推荐

