Algorithms: 散列表
来源:互联网 发布:淘宝上领的卷怎么使用 编辑:程序博客网 时间:2024/05/24 06:48
/*--------------------------------------------------------------------------------------- * 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 10struct 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;inti;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;intCollisionNum = 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");}
1 0
- Algorithms: 散列表
- 算法列表List of Algorithms
- Algorithms
- algorithms
- algorithms
- Algorithms
- ALGORITHMS
- Algorithms
- Algorithms
- STL Algorithms
- algorithms websites
- Approximation Algorithms
- Beginning Algorithms
- Miscellaneous algorithms
- Algorithms Compare
- Sorting Algorithms
- Practice Algorithms
- Dueling algorithms
- 堆放木块C++实现
- 2012年2月份(工作第1个月)工作总结及工作计划
- Sublime Text2的常用技巧总结(更新中...)
- CentOS忘记root密码解决办法
- Top 30 Phone Tech Interview Questions on Java
- Algorithms: 散列表
- java获取url地址后缀名
- 学习Java基础的目的?
- 《黑马程序员》 静态方法中不能new内部类的实例对象的总结
- 久邦数码Android开发工程师(3G&GO桌面)2014年3月份笔试-初面
- hdu 2586 lca模板题
- 无缝滚动通用JS代码
- 大型高性能ASP.NET系统架构设计
- USACO:1.4.2 The Clocks 时钟(IOI 94) 解析