typedef int KeyType;
typedef int ValueType;
typedef struct HashNode {
KeyType _key;
ValueType _value;
struct HashNode* _next;
}HashNode;
typedef struct HashTable {
HashNode** _tables;
size_t _size;
size_t _N;
}HashTable;
size_t GetNextPrimeSize(size_t cur);
size_t HashFunc(KeyType key,size_t size);
void HashTableInit(HashTable* ht);
int HashTableInsert(HashTable* ht, KeyType key, ValueType value);
HashNode* HashTableFind(HashTable* ht, KeyType key);
int HashTableRemove(HashTable* ht, KeyType key);
int HashTableDestory(HashTable* ht);
/
void HashTablePrint(HashTable* ht){
int i;
HashNode* cur;
assert(ht);
for(i=0;i<ht->_N;i++){
cur=ht->_tables[i];
if(cur!=NULL){
while(cur){
printf("[%d]%d ",i,cur->_key);
cur=cur->_next;
}
printf("
"); //一个链结束换行
}
}
printf("
");
}
size_t GetNextPrimeSize(size_t cur){
int i;
static const unsigned long _PrimeList [28] ={
53ul, 97ul, 193ul, 389ul, 769ul,
1543ul, 3079ul, 6151ul, 12289ul, 24593ul,
49157ul, 98317ul, 196613ul, 393241ul, 786433ul,
1572869ul, 3145739ul, 6291469ul, 12582917ul, 25165843ul,
50331653ul, 100663319ul, 201326611ul, 402653189ul, 805306457ul,
1610612741ul, 3221225473ul, 4294967291ul
};
for(i=0;i<28;i++){
if(cur<_PrimeList[i])
return _PrimeList[i];
}
return 0;
}
void HashTableInit(HashTable* ht){
size_t i;
assert(ht);
ht->_size=0;
ht->_N=GetNextPrimeSize(0);
ht->_tables=(HashNode**)malloc(sizeof(HashNode*)*(ht->_N));
memset(ht->_tables,NULL,sizeof(HashNode*)*(ht->_N));
}
size_t HashFunc(KeyType key,size_t N){
return key%N;
}
HashNode* BuyHashNode(KeyType key, ValueType value){
HashNode* node=(HashNode*)malloc(sizeof(HashNode));
node->_key=key;
node->_value=value;
node->_next=NULL;
return node;
}
int HashTableInsert(HashTable* ht, KeyType key, ValueType value){
size_t i,index,curN,newN,newIndex;
HashNode* node, *cur,*next,**newTable,*parent;
assert(ht);
//增容问题
if(ht->_size==ht->_N){
curN=ht->_N;
newN=GetNextPrimeSize(ht->_size);
newTable=(HashNode**)malloc(sizeof(HashNode*)*newN);
for(i=0;i<curN;i++){
cur=ht->_tables[i];
while(cur){
next=cur->_next;
newIndex=HashFunc(cur->_key,newN);
cur->_next=newTable[newIndex];
newTable[newIndex]=cur;
cur=next;
}
}
}
index=HashFunc(key,ht->_N);
cur=ht->_tables[index];
//next=ht->_tables[index];
if(cur==NULL){ //根据cur是否是头,插入
ht->_tables[index]=BuyHashNode(key,value);
return 1;
}
else{
while(cur){
if(cur->_key==key)
return -1;
parent=cur; //两种方法,一种头插,一种用链表的方法进行尾插
cur=cur->_next;
}
node=BuyHashNode(key,value);
parent->_next=node;
//node->_next=next;
//ht->_tables[index]=node;
return 1;
}
}
HashNode* HashTableFind(HashTable* ht, KeyType key){
int i,index;
HashNode* cur;
assert(ht);
index=HashFunc(key,ht->_N);
cur=ht->_tables[index];
while(cur){
if(cur->_key==key)
return cur;
cur=cur->_next;
}
return NULL;
}
int HashTableRemove(HashTable* ht, KeyType key){
HashNode* cur,*next;
assert(ht);
cur=HashTableFind(ht,key);
if(cur){
if(cur->_next==NULL){
ht->_tables[HashFunc(key,ht->_N)]=NULL;
free(cur);
ht->_size--;
}
else{
next=cur->_next;
cur->_key=next->_key;
cur->_value=next->_value;
cur->_next=next->_next;
next=NULL;
free(next);
}
}
return 0;
}
int HashTableDestory(HashTable* ht){
size_t i;
HashNode* cur,*next;
assert(ht);
for(i=0;i<ht->_N;i++){
cur=ht->_tables[i];
while(cur){
next=cur->_next;
free(cur);
cur=next;
}
}
free(ht->_tables);
ht->_tables=NULL;
}
void TestHashTableLink(){
HashTable ht;
HashTableInit(&ht);
HashTableInsert(&ht,2,10);
HashTableInsert(&ht,55,30);
HashTableInsert(&ht,25,15);
HashTableInsert(&ht,3,60);
HashTableInsert(&ht,3,90);
HashTableInsert(&ht,56,70);
HashTableInsert(&ht,109,80);
HashTableInsert(&ht,7,66);
HashTablePrint(&ht);
printf("Find 2 KEY %d
",HashTableFind(&ht,2)->_key);
HashTableRemove(&ht,25);
HashTablePrint(&ht);
HashTableDestory(&ht);
}