CS50 Week5 拼写者
我正在研究 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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论