C语言实现Hash表
#include<stdio.h>
#include "uthash.h"
struct hash_node
{
int key;
char val[20];
UT_hash_handle hh;
};
struct hash_node* head = NULL;
struct hash_node* get(const int key)
{
struct hash_node* node = NULL;
HASH_FIND_INT(head, &key, node);
return node;
}
void put(const int key, const char* val)
{
struct hash_node* node = get(key);
if(!node)
{
node = (struct hash_node*)malloc(sizeof(struct hash_node));
node->key = key;
strncpy(node->val, val, sizeof(node->val));
HASH_ADD_INT(head, key, node);
}
}
void delete(const int key)
{
struct hash_node* node = get(key);
if(node)
{
HASH_DEL(head, node);
free(node);
}
}
void clear()
{
struct hash_node* index = head;
for(;index;index = index->hh.next)
{
HASH_DEL(head, index);
free(index);
}
}
void print()
{
struct hash_node* index = head;
printf("{");
for(;index