gpt4 book ai didi

c - 如何将单词插入简单的哈希函数 C

转载 作者:太空宇宙 更新时间:2023-11-04 03:12:32 25 4
gpt4 key购买 nike

新手,试图弄清楚如何将 1 个单词插入到一个简单的哈希表(已给出)中。这个词是小写的。但是,我收到以下错误:

""exit status 1
main.c:30:40: error: function definition is not allowed here
unsigned int hash(const char *word){""


#include <ctype.h>
#include <stdbool.h>
#include <stdio.h>

int main(void) {
int N = 26;
// Length = 45;

// Represents a node in a hash table
typedef struct node
{
// LENGTH is defined in dictionary.h as 45, max length
char word[45+1];
struct node *next;
}
node;

// Represents a hash table, N is defined as 26 above
node *hashtable[N];

// Initialize hash table
for (int i = 0; i < N; i++)
{
hashtable[i] = NULL;
}

char *word = "jello";

// Hashes word to a number between 0 (a) and 25 (z), inclusive, based on its first letter, creating 26 buckets, or alphabetized categories

unsigned int hash(const char *word){
return tolower(word[0]) - 'a';
}

int x = word[0] - 'a';

printf("%d\n", x);
printf("%s\n", word);
printf("%c\n", word[0]);

return 0;
}

最佳答案

如错误消息所示,您在不应该定义的地方定义函数 - 在本例中,在 main 函数内。将它移出那里,您应该没问题。

关于c - 如何将单词插入简单的哈希函数 C,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55011238/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com