附上代码:
头文件
#ifndef __HASHTABLE_H__
#define __HASHTABLE_H__
#include <stdio.h>
#include <assert.h>
#include <stdlib.h>
enum State
{
EMPTY,
EXIST,
DELETE
};
typedef int HTKeyType;
typedef int HTValueType;
typedef struct HashNode
{
enum State _state;
HTKeyType _key;
HTValueType _value;
}HashNode;
typedef struct HashTable
{
HashNode* _table; //可增容的hash的表
int _len; //表长
int _size; //大小
}HashTable;
void HTInit(HashTable* ht, int len);
void HTDestroy(HashTable* ht);
int HTInsert(HashTable* ht, HTKeyType key, HTValueType value);
HashNode* HTFind(HashTable* ht, HTKeyType key);
int HTRemove(HashTable* ht, HTKeyType key);
int HTSize(HashTable* ht);
int HTEmpty(HashTable* ht);
void HTPrint(HashTable* ht);
#endif //__HASHTABLE_H__
接口实现
#include "HashTable.h"
void HTInit(HashTable* ht, int len)
{
assert(ht);
ht->_table = (HashNode*)malloc(sizeof(HashNode) * len);
if (ht->_table == NULL)
{
perror("use malloc!");
}
//将所有的hash结点状态置为空
for (int i = 0; i < len; i++)
{
ht->_table[i]._state = EMPTY;
}
ht->_len = len;
ht->_size = 0;
}
void HTDestroy(HashTable* ht)
{
assert(ht);
ht->_table = NULL;
ht->_len = 0;
ht->_size = 0;
}
//除留余数法
int HashFunc(HTKeyType key, int len)
{
return key % len;
}
//增容
//思想:重新申请一个新的哈希表,将旧表的数据放在新表,然后删除新表
void AddCapacity(HashTable* ht)
{
int i = 0;
assert(ht);
HashTable newTable;
newTable._len = ht->_len * 2;
HTInit(&newTable, newTable._len);
//将旧的表的值给新表
while (ht->_table[i]._state != EMPTY)
{
int index = HashFunc(ht->_table[i]._key, ht->_len);
newTable._table[index]._key = ht->_table[i]._key;
newTable._table[index]._value = ht->_table[i]._value;
newTable._table[index]._state = EXIST;
}
//再将新表赋值给旧表
free(ht->_table);
ht->_table = newTable._table;
ht->_len = newTable._len;
ht->_size = newTable._size;
HTDestroy(&newTable);
}
int HTInsert(HashTable* ht, HTKeyType key, HTValueType value)
{
assert(ht);
//判断是否满,运用装载因子
if (ht->_size * 10 /ht->_len > 7)
{
AddCapacity(ht);
}
//插入
int index = HashFunc(key, ht->_len);
//解决冲突
while (ht->_table[index]._state == EXIST)
{
if (ht->_table[index]._key == key)
{
return 0;
}
else
{
index++;
}
}
//找到要插入的位置
ht->_table[index]._key = key;
ht->_table[index]._value = value;
ht->_table[index]._state = EXIST;
ht->_size++;
return 1;
}
HashNode* HTFind(HashTable* ht, HTKeyType key)
{
assert(ht);
int index = HashFunc(key, ht->_len);
while (ht->_table[index]._state != EMPTY)
{
//判断查找的数据存在还是删除
if (ht->_table[index]._key == key)
{
//存在
if (ht->_table[index]._state == EXIST)
{
return &ht->_table[index];
}
//删除
else
{
return NULL;
}
}
//继续向后探测
else
{
index++;
}
}
//没有找到
return NULL;
}
//删除一个结点只是将结点状态变为DELETE,而不是将数据进行删除了
int HTRemove(HashTable* ht, HTKeyType key)
{
assert(ht);
//哈希表为空无法删除
if (ht->_table == NULL)
{
return 0;
}
HashNode* aim_node = HTFind(ht, key);
//找到要删除的结点
if (aim_node)
{
aim_node->_state = DELETE;
ht->_size--;
return 1;
}
return 0;
}
int HTSize(HashTable* ht)
{
assert(ht);
return ht->_size;
}
int HTEmpty(HashTable* ht)
{
assert(ht);
return ht->_size == 0 ? 0 : 1;
}
void HTPrint(HashTable* ht)
{
assert(ht);
for (int i = 0; i < ht->_len; i++)
{
if (ht->_table[i]._state == EXIST)
{
printf("%d:%d\n", ht->_table[i]._key, ht->_table[i]._value);
}
}
printf("\n");
}
测试函数
#include "HashTable.h"
void TsetHashtable()
{
HashTable table;
HTInit(&table, 5);
HTInsert(&table, 11, 1);
HTInsert(&table, 13, 2);
HTInsert(&table, 21, 3);
HTInsert(&table, 22, 4);
HTRemove(&table, 22);
HashNode* ret = HTFind(&table, 22);
if (ret)
{
printf("%d,%d,%d\n", ret->_state, ret->_key, ret->_value);
}
HTPrint(&table);
printf("大小:%d\n", HTSize(&table));
printf("是否为空:%d\n", HTEmpty(&table));
HTRemove(&table, 11);
HTPrint(&table);
HTDestroy(&table);
}
int main()
{
TsetHashtable();
return 0;
}