包含单词链表的哈希 table C
Hash table containing linked lists of words C
我完全不熟悉 C,所以我在使用散列 table 和链表时遇到了麻烦。
下面是我的代码:
typedef struct WordList {
char *word;
struct WordList *next;
} WordList;
typedef struct HashTable {
int key;
struct WordList value;
} HashTable;
#define TABLE_SIZE 10
HashTable *table[TABLE_SIZE] = { NULL };
//Insert element
void insertElement(int key, char *word) {
int i = 0;
// check if key has already existed
while((table[i]->key != 0) && (i < TABLE_SIZE)) {
if(table[i]->key == key) { // if find key
struct WordList wl = table[i]->value;
// assign word to temp list then append to existed list
struct WordList temp = (struct WordList *)malloc(sizeof(struct WordList));
temp->word = word;
temp->next = wl;
wl = temp;
table[i]->value = wl; // add word to existed linked list
return; // exit function and skip the rest
}
i++; // increment loop index
}
// find a NULL slot and store key and value
if(table[i]->key == NULL) {
table[i]->key = key;
struct WordList wl = table[i]->value;
// assign word to temp list then append to existed list
struct WordList temp = (struct WordList *)malloc(sizeof(struct WordList));
temp->word = word;
temp->next = wl;
wl = temp;
table[i]->value == wl;
}
}
int main() {
// test call
insertElement(1, "blah[=11=]");
int i;
for ( i=0; i < 10; i++)
{
printf("%d: ", i);
struct HashTable *tableTemp = table[i];
while (entryTemp != NULL)
{
printf("(%d)\n", tableTemp->key);
tableTemp = tableTemp->next;
}
printf("\n");
}
return 0;
}
我收到这个错误:
In function ‘insertElement’:
error: invalid initializer
struct WordList temp = (struct WordList *)malloc(sizeof(struct WordList));
^
你能告诉我我搞砸了哪一部分吗?
我知道这里的编码风格也很糟糕,但是你能先解决实际问题然后再评论其余的吗?
struct WordList temp = (struct WordList *)malloc(sizeof(struct WordList));
注意左边是结构体,右边是结构体
是一个指针。你可能想要:
struct WordList *temp = (struct WordList *)malloc(sizeof(struct WordList));
我完全不熟悉 C,所以我在使用散列 table 和链表时遇到了麻烦。 下面是我的代码:
typedef struct WordList {
char *word;
struct WordList *next;
} WordList;
typedef struct HashTable {
int key;
struct WordList value;
} HashTable;
#define TABLE_SIZE 10
HashTable *table[TABLE_SIZE] = { NULL };
//Insert element
void insertElement(int key, char *word) {
int i = 0;
// check if key has already existed
while((table[i]->key != 0) && (i < TABLE_SIZE)) {
if(table[i]->key == key) { // if find key
struct WordList wl = table[i]->value;
// assign word to temp list then append to existed list
struct WordList temp = (struct WordList *)malloc(sizeof(struct WordList));
temp->word = word;
temp->next = wl;
wl = temp;
table[i]->value = wl; // add word to existed linked list
return; // exit function and skip the rest
}
i++; // increment loop index
}
// find a NULL slot and store key and value
if(table[i]->key == NULL) {
table[i]->key = key;
struct WordList wl = table[i]->value;
// assign word to temp list then append to existed list
struct WordList temp = (struct WordList *)malloc(sizeof(struct WordList));
temp->word = word;
temp->next = wl;
wl = temp;
table[i]->value == wl;
}
}
int main() {
// test call
insertElement(1, "blah[=11=]");
int i;
for ( i=0; i < 10; i++)
{
printf("%d: ", i);
struct HashTable *tableTemp = table[i];
while (entryTemp != NULL)
{
printf("(%d)\n", tableTemp->key);
tableTemp = tableTemp->next;
}
printf("\n");
}
return 0;
}
我收到这个错误:
In function ‘insertElement’:
error: invalid initializer
struct WordList temp = (struct WordList *)malloc(sizeof(struct WordList));
^
你能告诉我我搞砸了哪一部分吗? 我知道这里的编码风格也很糟糕,但是你能先解决实际问题然后再评论其余的吗?
struct WordList temp = (struct WordList *)malloc(sizeof(struct WordList));
注意左边是结构体,右边是结构体 是一个指针。你可能想要:
struct WordList *temp = (struct WordList *)malloc(sizeof(struct WordList));