/*哈希表的实现(字符串作为key)*/
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#define HASH_TABLE_LENGTH 100
//哈希表头
typedef struct HASH_HEAD
{
struct HASH_LINK *next;
}Hash_head;
//单链表,存储数据
typedef struct HASH_LINK
{
char * key_value;
struct HASH_LINK *next;
}Hash_link;
//哈希表
Hash_head * Hash_tab;
//初始化哈希表
int Init_Hash_head()
{
int i;
Hash_head *head;
Hash_tab = (Hash_head *)malloc(sizeof(Hash_head) * HASH_TABLE_LENGTH);
if(Hash_tab == NULL)
{
return -1;
}
for(i = 0; i < HASH_TABLE_LENGTH; i++)
{
head = &Hash_tab[i];
head->next = NULL;
}
return 0;
}
//初始化链表结点
Hash_link *Init_Hash_link()
{
Hash_link *node;
node = (Hash_link *)malloc(sizeof(Hash_link));
if(node == NULL)
{
return NULL;
}
node->next = NULL;
return node;
}
//哈希函数
int Hash_func(char *key_value)
{
int i;
int addr = 0;
for(i = 0;i<strlen(key_value);i++)
{
addr = addr + key_value[i];
}
addr = addr % HASH_TABLE_LENGTH; //对哈希表长度取余
return addr;
}
//插入结点
/*
int Hash_insert(char *key_value)
{
int pos;
Hash_link * node;
Hash_link * Curnode;
printf("***Hash_insert start***");
node = Init_Hash_link();
node->key_value = key_value;
pos = Hash_func(key_value);
printf("pos=%d",pos);
Curnode = Hash_tab[pos]->next;
if(Curnode == NULL) //该位置链表为空
{
Curnode = node;
printf("*****Hash_insert end*****");
return 0;
}
while(Curnode->next != NULL)
{
Curnode=Curnode->next;
}
Curnode->next = node;
printf("***Hash_insert end***");
return 0;
}
*/
//插入结点
int Hash_insert(Hash_link *newnode)
{
int pos;
Hash_link * Curnode;
Hash_head *head;
printf("***Hash_insert start***\n");
pos = Hash_func(newnode->key_value);
printf("pos=%d\n",pos);
head = &Hash_tab[pos];
if(head->next == NULL) //该位置链表为空
{
head->next = newnode;
newnode->next = NULL;
printf("*****Hash_insert end*****\n");
return 0;
}
Curnode = head->next;
while(Curnode->next != NULL)
{
Curnode=Curnode->next;
}
Curnode->next = newnode;
newnode->next = NULL;
printf("***Hash_insert end***\n");
return 0;
}
//按key查找结点
Hash_link *Hash_search(char *key_value)
{
int pos;
Hash_link *Curnode;
printf("***Hash_search start***\n");
pos = Hash_func(key_value);
printf("search:pos=%d\n",pos);
if(Hash_tab[pos].next == NULL)
{
printf("can not find the key\n");
printf("***Hash_search end*\n");
return NULL;
}
Curnode = Hash_tab[pos].next;
if(!strcmp(Curnode->key_value,key_value))
{
printf("find out the first key:%s\n",Curnode->key_value);
printf("***Hash_search end*\n");
return Curnode; //如果是第一个结点时返回该结点
}
while(Curnode->next != NULL)
{
if(!strcmp(Curnode->next->key_value,key_value))
{
printf("find out the key:%s\n",Curnode->next->key_value);
printf("***Hash_search end**\n");
return Curnode; //不是第一个结点时返回查找结点的前一个结点,方便删除
}
Curnode = Curnode->next;
}
printf("can not find the key-end\n");
printf("***Hash_search end***\n");
return NULL;
}
//删除结点
int Hash_delete(Hash_link *newnode)
{
Hash_link *delnode;
Hash_link *firstnode;
Hash_link *node;
int pos;
printf("****Hash_delete start****\n");
pos = Hash_func(newnode->key_value);
firstnode = Hash_tab[pos].next;
node = Hash_search(newnode->key_value);
if(node == NULL)
{
printf("***Hash_delete:Did not find the node");
return -1;
}
//判断要删除的是否是第一个结点
if(!strcmp(firstnode->key_value,newnode->key_value)) //是第一个结点
{
Hash_tab[pos].next == newnode->next;
free(node);
node = NULL;
printf("****Hash_delete end*\n");
return 0;
}
else //不是第一个结点
{
delnode = node->next;
node->next = delnode->next;
free(delnode);
delnode = NULL;
printf("***Hash_delete end**\n");
return 0;
}
}
int main()
{
char key_value[256];
int addr;
Hash_link *newnode;
//初始化哈希表头
Init_Hash_head();
//初始化链表结点
newnode = Init_Hash_link();
newnode->key_value = "cpf";
Hash_insert(newnode);
newnode = Init_Hash_link();
newnode->key_value = "fpc";
Hash_insert(newnode);
Hash_search("fpc");
newnode = Init_Hash_link();
newnode->key_value = "fpc";
Hash_delete(newnode);
return 0;
}