CS50 Week5 拼写者

发布于 2025-01-11 07:02:03 字数 1047 浏览 2 评论 0原文

我正在研究 CS50 week5 问题拼写器。我想我对加载功能有些疑问。我的意思是,在我的代码中,在对单词进行哈希处理后,我将单词加载到相应链表的头部。我的代码有效。不过,我想我也可以将这个词加载到列表的末尾。问题是我不知道如何更改我的代码来做到这一点。你能帮助我吗?下面是我的加载函数的代码。

// Read through the dictionary word by word
while (fscanf(input, "%s\n", currWord) != EOF)
{
    // Create a temporary node to copy the word
    node *tempNode = malloc(sizeof(node));
    if (tempNode == NULL)
    {
        fclose(input);
        return false;
    }
    strcpy(tempNode->word, currWord);

    // Get the key for that word
    int key = hash(currWord);

    // Load the word to the head of the corresponding linked-list
    if (table[key] == NULL)
    {
        tempNode->next = NULL;
        table[key] = tempNode;
    }
    else
    {
        tempNode->next = table[key];
        table[key] = tempNode;
    }

    // Count the number of loaded words
    wordCount++;
}

// Close the dictionary
fclose(input);
return true;
}

如果您需要更多信息,请告诉我!谢谢!!!

I am working on CS50 week5 problem speller. I think I have some doubt about the load function. I mean, in my code, after hashing the word, I loaded the word to the head of the corresponding linked-list. And my code worked. However, I think I can also load the word to the end of the list. The problem is that I don't know how I can change my code to do that. Can you help me? Below is my code of the load funtion.

// Read through the dictionary word by word
while (fscanf(input, "%s\n", currWord) != EOF)
{
    // Create a temporary node to copy the word
    node *tempNode = malloc(sizeof(node));
    if (tempNode == NULL)
    {
        fclose(input);
        return false;
    }
    strcpy(tempNode->word, currWord);

    // Get the key for that word
    int key = hash(currWord);

    // Load the word to the head of the corresponding linked-list
    if (table[key] == NULL)
    {
        tempNode->next = NULL;
        table[key] = tempNode;
    }
    else
    {
        tempNode->next = table[key];
        table[key] = tempNode;
    }

    // Count the number of loaded words
    wordCount++;
}

// Close the dictionary
fclose(input);
return true;
}

If you need more information, please tell me! THANKS!!!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文