我正在尝试按一个字段(id
)搜索一个struct one记录数组。
代码编译,但不起作用。我认为这是创建哈希表的问题。但无法解决问题,我是一个哈希表的初学者,是否可以创建一个大小为元素的哈希表?无论如何,访问的时间是 O(1)所以我只使用了很多内存吗?
这里有我的完整代码,提前感谢每个建议/评论。
这些是我的理由:
typedef struct _SPerson {
char name[20];
char surname[20];
char id[20];
char telephone[20];
} SPerson;
typedef struct SInfo {
int key;
SPerson value;
} TInfo;
struct SNode_list { /*list node*/
TInfo information; /*data->key and value*/
struct LNode *next; /*pointer to next element*/
};
typedef struct SNode_list LNode; // LNode single element of list
typedef LNode *List;
/*hash table struct*/
typedef struct _HashTable {
int bucket_number;
List *bucket;
} HashTable;
我有一个加载了20000条记录的数组
SPerson Archive[20000];
在我搜索数据的功能中,我这样做:
void search_using_hash_table(SPerson Archive[], int ne) {
HashTable *ht = hashtable_create(ne);
SPerson *app_record
char *app = get_string();
for (i = 0; i < ne; i++) /*Creating Hash-Table*/
hashtable_insert(ht, i, Archive[i]);
app_record = hashtable_search(ht, app);
if (app != NULL)
printf("\n\nFound.\n %s %s %s %s\n",
app->id, app->surname, app->name, app->telephone);
else
printf("\n\nNot found.\n");
}
这些是我的功能:
插入
void hashtable_insert(HashTable *ht, int key, SPerson value) {
TInfo info;
LNode *node;
unsigned h;
info.key = key;
info.value = value;
h = hash(value.id) % ht->bucket_number;
node = list_search_unordered(ht->bucket[h], info);
if (node == NULL) /*no collision*/
ht->bucket[h] = list_insert_at_index(ht->bucket[h], info);
else /*push*/
node->information = info;
}
搜索
SPerson *hashtable_search(HashTable *ht, char *key) {
unsigned h = hash(key) % ht->bucket_number;
TInfo info;
info.key = key;
LNode *node = list_search_unordered(ht->bucket[h], info);
if (node == NULL)
return NULL;
else
return &node->information.value;
}
列出搜索
LNode *list_search_unordered(List list, TInfo info) {
LNode *curr;
curr = list;
while ((curr != NULL) && !equal(info, curr->information)) {
curr = curr->next;
}
if (curr == NULL)
return NULL;
else
return curr;
}
等于
bool equal(TInfo a, TInfo b) {
return a.key == b.key;
}
创建
HashTable *hashtable_create(int buckets) {
int i;
HashTable *p = (HashTable *)malloc(sizeof(HashTable)); /*allocation*/
assert(p != NULL); /*assert verificate if allocation went ok*/
assert(buckets > 0);
p->bucket_number = buckets;
p->bucket = (List *)malloc(sizeof(List)*buckets);
assert(p->bucket != NULL);
for (i = 0; i < buckets; i++)
p->bucket[i] = NULL;
return p;
}
哈希算法
unsigned long hash(unsigned char *str) { /*djb algorithm*/
unsigned long hash = 5381;
int c;
while (c = *str++)
hash = ((hash << 5) + hash) + c; /* hash * 33 + c */
return hash;
}
答案 0 :(得分:3)
您在存储时对ids
进行哈希处理,并且在搜索时使用散列key
来搜索尚未进行哈希处理的内容,以便它们永远不会相等。
void hashtable_insert(HashTable *ht, int key, SPerson value)
{
TInfo info;
LNode *node;
unsigned h;
info.key = key;
info.value = value;
h = hash(value.id) % ht->bucket_number;...
SPerson *hashtable_search(HashTable *ht, char* key)
{
unsigned h = hash(key) % ht->bucket_number;
TInfo info;
info.key = key;
LNode *node = list_search_unordered(ht->bucket[h], info);...