【问题标题】:CS50 Pset 5 Hashtable issueCS50 Pset 5 哈希表问题
【发布时间】:2020-06-14 04:53:49
【问题描述】:

在创建哈希表并将每个字母分配给表的值后,我注意到表输出的第一个单词,因为每个链表的开头都是同一个单词。不知何故,即使我试图将它们分开,我似乎也在将整个字典转移到表中的每个数组中。任何帮助都会很棒!提前致谢

// Implements a dictionary's functionality

#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include <string.h>
#include "dictionary.h"

// Represents a node in a hash table
typedef struct node
{
    char *word;
    struct node *next;
}
node;

// Number of buckets in hash table
const unsigned int N = 25;

// Hash table
node *table[N];

char lowerword[LENGTH+1];
// Returns true if word is in dictionary else false
bool check(const char *word)
{
    int bucketfind = 0;
    int x = 0;
    for (int b = word[x]; b != '\0';b = word[x], x++)
    {
        int lowertemp = tolower(word[x]);
        if (x == 0)
        {
            bucketfind = lowertemp - 97;
        }
        char lowerfinal = lowertemp;
        lowerword[x] = lowerfinal;

        //printf("%c", lowerword[x]);
    }
    int wordlen = x + 1;
    int pr = 0;
    while (table[bucketfind] -> next != NULL)
    {
        int dwlen = strlen(table[bucketfind]-> word);
        pr++;
        //printf("%i, %i, %s, %i\n", pr, dwlen, table[bucketfind] -> word, bucketfind);
    }
    //TODO
    return false;
}

// Hashes word to a number
unsigned int hash(const char *word)
{
    int asciifirst = word[0];
    int lowerfirst = tolower(asciifirst);
    int bucketnum = lowerfirst - 97;
    return bucketnum;
}

// Loads dictionary into memory, returning true if successful else false
int dictwords = 0;
//char *cword = (char*)malloc(sizeof(char)*46);
bool load(const char *dictionary)
{
    char *cword = malloc(sizeof(char)*46);
    FILE *dict = fopen(dictionary, "r");
    if (dictionary == NULL)
    {
        return false;
    }
    int x = 0;
    while ((fscanf(dict, "%s", cword) != EOF))
    {
        node *nword = malloc(sizeof(node));
        nword -> word = cword;
        nword -> next = NULL;

        int bucket = hash(cword);
        //printf("%i\n", bucket);
        if (table[bucket] != NULL)
        {
            nword -> next = table[bucket];
            table[bucket] = nword;
        }
        else
        {
            table[bucket]= nword;
        }
        dictwords++;
    }
    fclose(dict);
return true;
}

// Returns number of words in dictionary if loaded else 0 if not yet loaded
unsigned int size(void)
{
    return dictwords;
}

// Unloads dictionary from memory, returning true if successful else false
bool unload(void)
{
    // TODO
    return false;
}

【问题讨论】:

    标签: hashtable cs50


    【解决方案1】:

    这不仅仅是第一个词; 链表中的每个词都是同一个词(最后一个读)。 cword 在此处 char *cword = malloc(sizeof(char)*46); 的特定地址处获得 46 字节的内存。字典中的每个单词都被读入同一个地址。

    【讨论】:

    • 我理解我的问题,如果正确分配内存,我会处理,但是当你说每个单词都是最后一次阅读时,我每次都打印出我的“cwords”,它确实返回了字典。试图弄清楚,但如果你能澄清会很棒!
    • 我想我明白了...通过声明节点字等于 cword,我未能为节点本身中的字分配内存,并且每次“cword”在内存中发生变化时,也是如此与它相等的节点。为了解决这个问题,我需要为节点中的每个单词分配内存并实际复制当前单词,这样每次 cword 都不会改变它。乍一看,输出似乎更正确,但确认我理解这一点
    • 理解似乎是有效的。
    • 非常感谢您抽出宝贵时间帮助我解决这个问题!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-07-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-10-07
    相关资源
    最近更新 更多