如何解决cs50 拼写问题哈希表似乎没有在加载函数中形成
目前正在做cs50 pset5的拼写问题。
我在形成哈希表时遇到问题,我认为它会导致稍后当我尝试运行搜索该表的函数时出现分段错误。
这是创建哈希表的函数:
bool load(const char *dictionary)
{
FILE *dict = fopen(dictionary,"r"); //opens dictionary file
if (dict == NULL) // if cant be opened loading Failed
{
return false;
}
char w[LENGTH + 1]; //buffer (length is the maximum character number
int i = 0; //index within word
while(fscanf(dict,"%s",w) != EOF) // scanning the dictionary for words
{
int x = hash(w); //getting the number of the linked list within the table
node *n = malloc(sizeof(node)); //allocating memory for a new node
if (n == NULL)
{
return false;
}
for (int j = 0; j < strlen(w) + 1; j++) // documenting the word within the new node
{
n->word[i] = w[j];
}
n->next = table[x];
table[x] = n; //new node is the beginning of the linked list
dicsize++;
}
fclose(dict);
return true;
}
我的主要问题是我的表格代码是否正确,如果不正确,那么为什么 先感谢您 这是整个代码:
// Implements a dictionary's functionality
#include <string.h>
#include <strings.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include "dictionary.h"
// Represents a node in a hash table
typedef struct node
{
char word[LENGTH + 1];
struct node *next;
}
node;
// Number of buckets in hash table
const unsigned int N = 26;
int dicsize = 0;
// Hash table
node *table[N];
// Returns true if word is in dictionary,else false
bool check(const char *word)
{
int x = hash(word);
node *cur = table[x];
while(table[x] != NULL)
{
if(strcasecmp(word,cur->word) == 0)
{
return true;
}
if(cur == NULL)
{
return false;
}
cur = cur->next;
}
return false;
}
// Hashes word to a number
unsigned int hash(const char *word)
{
char temp = tolower(word[0]);
int place = (temp - 97);
return place;
}
// Loads dictionary into memory,returning true if successful,else false
bool load(const char *dictionary)
{
FILE *dict = fopen(dictionary,"r"); //opens dictionary file
if (dict == NULL) // if cant be opened loading Failed
{
return false;
}
char w[LENGTH + 1]; //buffer
int i = 0; //index within word
while(fscanf(dict,w) != EOF) // scanning the dictionary for words
{
int x = hash(w); //getting the number of the linked list within the table
node *n = malloc(sizeof(node)); //allocating memory for a new node
if (n == NULL)
{
return false;
}
for (int j = 0; j < strlen(w) + 1; j++) // documenting the word within the new node
{
n->word[i] = w[j];
}
n->next = table[x];
table[x] = n; //new node is the beginning of the linked list
dicsize++;
}
fclose(dict);
return true;
}
// Returns number of words in dictionary if loaded,else 0 if not yet loaded
unsigned int size(void)
{
printf("%i",dicsize);
return dicsize;
}
// Unloads dictionary from memory,else false
bool unload(void)
{
// Todo
return false;
}
解决方法
可能的罪魁祸首是
for (int j = 0; j < strlen(w) + 1; j++) // documenting the word within the new node
{
n->word[i] = w[j];
}
由于 i
在循环内没有改变,它不会复制字符串,而是将所有字符转储为一个。另一个索引也应该是 j
。 (如n->word[j] = w[j];
)
无论如何最好以 strcpy
的形式执行此操作。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。