I have a program in C that creates a hash table. I want to know what it is, but I am unsure how to print it out or display it. I have pasted the program below. I am rather new to hash tables so any help would be greatly appreciated!
我在C中有一个创建哈希表的程序。我想知道它是什么,但我不确定如何打印或显示它。我已粘贴下面的程序。我是哈希表的新手,所以任何帮助都将不胜感激!
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#define TABLE_SIZE 7
#define NUM_INPUTS 7
int hash( char *s )
/* Note, this is a horrible hash function. It's here for
instructional purposes */
{
return strlen( s ) % TABLE_SIZE ;
}
typedef struct entry
{
char *key;
int val;
struct entry *next;
} entry;
entry* table[ TABLE_SIZE ] = { NULL };
void insert( char *s, int v )
/* this insert is NOT checking for duplicates. :/ */
{
int h = hash( s );
entry *t = (entry*) malloc( sizeof( entry ));
t->key = s;
t->val = v;
t->next = table[h];
table[h] = t;
}
void clean_table()
{
entry *p, *q;
int i;
for( i=0; i<TABLE_SIZE; ++i )
{
for( p=table[i]; p!=NULL; p=q )
{
q = p->next;
free( p );
}
} // for each entry
} // clean_table
int main()
{
char* keyList[] = { "Jaga", "Jesse", "Cos", "Kate", "Nash", "Vera",
"Bob" };
int valList[] = { 24, 78, 86, 28, 11, 99, 38 };
int i;
for( i=0; i<NUM_INPUTS; ++i )
insert( keyList[i], valList[i] );
/* what does the table look like here? */
clean_table();
return( 0 );
}
2 个解决方案
#1
As requested in the comments, a search function as desired would look like this:
根据评论中的要求,所需的搜索功能如下所示:
int search(const char* key, int* out_val)
{
// Find the hash index into the table.
const int index = hash(key);
// Now do a linear search through the linked list.
for (struct entry* node = table[index]; node; node = node->next)
{
// If we find a node with a matching key:
if (strcmp(node->key, key) == 0)
{
// Output the value and return 1 for success.
*out_val = node->val;
return 1;
}
}
// We didn't find anything. Return 0 for failure.
return 0;
}
#2
Here's an example of a simple Hash Table in C. It doesn't really do any error handling, so this is not at all suitable for production, but it should help to see an example of a working implementation. Here is another post helping someone work through a hash table implementation in C.
这是C中一个简单哈希表的示例。它实际上没有进行任何错误处理,因此这根本不适合生产,但它应该有助于查看工作实现的示例。这是另一篇帮助某人通过C中的哈希表实现的帖子。
#1
As requested in the comments, a search function as desired would look like this:
根据评论中的要求,所需的搜索功能如下所示:
int search(const char* key, int* out_val)
{
// Find the hash index into the table.
const int index = hash(key);
// Now do a linear search through the linked list.
for (struct entry* node = table[index]; node; node = node->next)
{
// If we find a node with a matching key:
if (strcmp(node->key, key) == 0)
{
// Output the value and return 1 for success.
*out_val = node->val;
return 1;
}
}
// We didn't find anything. Return 0 for failure.
return 0;
}
#2
Here's an example of a simple Hash Table in C. It doesn't really do any error handling, so this is not at all suitable for production, but it should help to see an example of a working implementation. Here is another post helping someone work through a hash table implementation in C.
这是C中一个简单哈希表的示例。它实际上没有进行任何错误处理,因此这根本不适合生产,但它应该有助于查看工作实现的示例。这是另一篇帮助某人通过C中的哈希表实现的帖子。