首页 > 代码库 > 算法:开放定址法散列表
算法:开放定址法散列表
hash.h
#ifndef _HASHQUAD_H #define _HASHQUAD_H #define MinTableSize 10 struct HashEntry; struct HashNode; typedef char *ElementType; typedef unsigned int Index; typedef Index Position; typedef struct HashNode *HashTable; typedef struct HashEntry *Cell; enum KindOfEntry{Legitimate,Empty,Deleted}; HashTable InitializeHashTable(int TableSize); void DestoryTable(HashTable H); Position Find(ElementType key,HashTable H); void Insert(ElementType key,HashTable H); void Delete(ElementType key,HashTable H); Index Hash(const char *key,int TableSize); HashTable Rehash(HashTable H); #endif struct HashEntry { ElementType key; KindOfEntry info;//此处为懒惰删除域 }; struct HashNode { int TableSize; Cell *TheCells;//执行Cell的指针数组 };
hash.c
#include <stdlib.h> #include <stdio.h> #include "Hash1.h" //生成下个素数 static unsigned int NextPrime(int N) { if(N%2==0) N++; for(;;N+=2){ for(i=3;i*i<=N;i+=2){ if(N%i==0){ goto ContOuter; } } return N; ContOuter:; } } Index Hash(const char *key,int TableSize) { unsigned int HashVal=0; while(*key!=‘\0‘){ HashVal=(HashVal<<5)+*key++; } return HashVal%TableSize; } HashTable InitializeHashTable(int TableSize) { HashTable H; H=(HashTable)malloc(sizeof(struct HashNode)); if(H==NULL){ printf("out of space"); return NULL; } H->TableSize=NextPrime(TableSize); //分配地址空间 H->TheCells=malloc(sizeof(Cell))*H->TableSize; if(H->TheCells==NULL){ printf("out of space"); return NULL; } for(i=0;i<H->TableSize;++i){ H->TheCells[i]->info=Empty; } return H; } Position Find(ElementType key,HashTable H) { Position CurrentPos; int CollisionNum=0; CurrentPos=Hash(key,H); while(H->TheCells[CurrentPos]->info!=Empty&&strcmp(H->TheCells[CurrentPos].info,key)!=0){ //平方探测函数由 F(i) = F(i-1) + 2 * i - 1 CurrentPos+=2*++Collision-1; if(CurrentPos>TableSize){ CurrentPos-=TableSize; } } return CurrentPos; } void Insert(ElementType key,HashTable H) { Position Pos; Pos=Find(key,H); if(H->TheCells[Pos]->info!=Legitimate){ H->TheCells[Pos]->info=Legitimate; H->TheCells[Pos]->key=malloc(strlen(key)+1); strcpy(H->TheCells[Pos]->key,key); H->TheCells[Pos]->key[strlen(key)]=‘\0‘; } } void Delete(ElementType key,HashTable H) { Position Pos; Pos=Find(key,H); if(H->TheCells[Pos].info==Legitimate){ H->TheCells[Pos].info=Legitimate; } } void DestoryTable(HashTable H) { free(H->TheCells); free(H); } HashTable Rehash(HashTable H) { int i,OldSize; Cell *OldCells; OldCells=H->TheCells; OldSize=H->TableSize; H=IntializeHashTable(2*OldSize); //把原来表数据拷贝过来 for(i=0;i<OldSize;++i){ if(OldCells[i]->info==Legitimate){ Insert(OldCells[i]->info,H); } free(OldCells); } return H; }
算法:开放定址法散列表
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。