/*--------------------------------------------------------------------------------------- * Project: HashQuad.h * Name: zwp * Date: 2014.3 *----------------------------------------------------------------------------------------*/ #ifndef HASHQUAD_H_ #define HASHQUAD_H_ typedef int ElementType; typedef unsigned int Index; typedef Index Position; struct HashTbl; typedef struct HashTbl* HashTable; /* ** 初始化哈希表 */ HashTable InitializeTable(int TableSize); /* ** 遍历散列表 */ void Traverse(HashTable H); /* ** 销毁哈希表 */ void DestroyTable(HashTable H); /* ** 寻找元素 */ Position Find(ElementType Key, HashTable H); /* ** 插入元素 */ void Insert(ElementType Key, HashTable H); /* ** 删除元素 */ ElementType Retrive(Position P, HashTable H); /* ** 二次散列 */ HashTable Rehash(HashTable H); #endif
/*-------------------------------------------------------------------------- * Project: HashQuad.cpp * Name: zwp * Date: 2014/3 *---------------------------------------------------------------------------*/ #include "HashQuad.h" #include <stdlib.h> #include <stdio.h> #include <malloc.h> /* ** 表示三种状态, 已在, 空, 删除 */ enum KindOfEntry { Legitimate, Empty, Deleted }; #define MinTableSize 10 struct HashEntry { ElementType Element; enum KindOfEntry Info; }; typedef struct HashEntry Cell; struct HashTbl { Cell *TheCells; int TableSize; }; /* ** 判断是不是素数 */ bool isPrime(int number) { number = abs(number); if(number == 0 || number == 1) return true; int divisor; for(divisor = number/2; number % divisor != 0; -- divisor) ; return divisor == 1; } /* ** 产生素数 */ static int NextPrime(int num) { for(int index = num; index < 10000; ++ index) { if(num) return num; } return num; } /* ** 散列函数 */ int Hash(ElementType key, int size) { return (key % size); } /* ** 初始化散列表 */ HashTable InitializeTable(int TableSize) { HashTable H; int i; if(TableSize < MinTableSize) { printf("Table size too small....\n"); return NULL; } /* ** Allocate Table */ H = (HashTbl*)malloc(sizeof(struct HashTbl)); if(H == NULL) printf("Out of space.....\n"); H->TableSize = NextPrime(TableSize); /* Allocate array of Cells */ H->TheCells = (Cell*)malloc(sizeof(Cell) * H->TableSize); if(H->TheCells == NULL) printf("Out of space....\n"); /* Set all number state is ‘Empty’ */ 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->TableSize); while(H->TheCells[CurrentPos].Info != Empty && H->TheCells[CurrentPos].Element != Key) { /* Probably need strcmp ReHash */ CurrentPos += 2 * (++CollisionNum) - 1; if(CurrentPos >= H->TableSize) CurrentPos -= H->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].Element = Key; } } /* ** 删除元素 */ ElementType Retrive(Position P, HashTable H) { ElementType number = 0; if(H->TheCells[P].Info == Legitimate) { number = H->TheCells[P].Element; H->TheCells[P].Info = Empty; } return number; } /* ** 遍历散列表 */ void Traverse(HashTable H) { if(H->TheCells == NULL) printf("The Hash Table is Empty...\n"); for(int index = 0; index < H->TableSize; ++ index) printf("%d \n", H->TheCells[index].Element); } /* ** 销毁哈希表 */ void DestroyTable(HashTable H) { free(H->TheCells); free(H); } /* ** 二次散列 */ HashTable Rehash(HashTable H) { int i, OldSize; Cell* OldCells; OldCells = H->TheCells; OldSize = H->TableSize; /* Get a new, empty table */ H = InitializeTable(2 * OldSize); /* Scan through old table, reinsering into new */ for(i = 0; i < OldSize; ++ i) if(OldCells[i].Info == Legitimate) Insert(OldCells[i].Element, H); free(OldCells); return H; }
/*-------------------------------------------------------------------- * Project: Main.cpp * Name: zwp * Date: 2014/3 *--------------------------------------------------------------------*/ #include "HashQuad.h" #include <stdio.h> #include <stdlib.h> int main(int argc, char* argv[]) { HashTable H; H = InitializeTable(100); for(int i = 0; i < 100; ++ i) Insert(i+2, H); Traverse(H); system("pause"); }
Algorithms: 散列表,布布扣,bubuko.com
原文:http://blog.csdn.net/qqzwp/article/details/22483517