散列表的实现-开放定址法
2014-11-03 19:24
726 查看
散列表有两种实现方式,为分离链表法与开放定址法;其中以开放定址法需要选取适当的散列函数与冲突函数来实现,根据开放定址法散列函数,大致分为线性探测、平方探测、双散列等;
下面给出平方探测的开放定址法的实现:
下面给出平方探测的开放定址法的实现:
// hash: Open-addressing hasing typedef unsigned int Index; typedef Index Position; struct HashEntry; typedef struct HashEntry Cell; struct HashTbl; typedef struct HashTbl *HashTable; enum KindOfEntry { Legitimate, Empty, Delete }; struct HashEntry { ElementType element; enum KindOfEntry info; }; struct HashTbl { int tableSize; Cell *theCells; }; HashTable InitializeTable( int tableSize ) { HashTable H; if ( tableSize < MinTableSize ) { printf( "Table size too small" ); return NULL; } /* Allocate table */ H = (HashTable)malloc( sizeof(sizeof HashTbl) ); if ( H == NULL ) { printf( "Out of space" ); return NULL; } H->tableSize = NextPrime( tableSize ); /* Allocate array of cells */ H->theCells = (Cell *)malloc( sizeof(struct Cell) * H->talbeSize ); if ( H->theCells == NULL ) { printf( "Out of space" ); return NULL; } for ( int i = 0; i < H->tableSize; ++i ) H->theCells[i].info = Empty; return H; } void Find( ElementType key, HashTable H ) { // 不考虑表满情况 Position currentPos; int collisionNum; // 冲突次数 cullisionNum = 0; currentPos = Hash( key, H->tableSize ); while ( H->theCells[currentPos].Info != Empty && H->theCells[currentPos].element != key ) { currentPos += 2 * ++cullisionNum - 1; if ( currentPos >= H->tableSize ) cullisionNum -= H->tableSize; } return currentPos; } void Insert( ElementType key, HashTable H ) { Position p; p = Find( key, H ); if ( H->theCells[p].info != Legitimate ) { H->theCells[p].info = Legitimate; H->theCells[p].element = key; } }
相关文章推荐
- 散列 开放定址法 C实现
- 哈希(散列)表之开放定址法的C++类模板实现
- 开放定址散列表(平方探测法)实现文件C语言
- 再散列开放定址散列表(线性探测法)加强版可再散列经优化实现文件C语言
- 深入哈希表(二)--开放定址法实现哈希表
- 数据结构与算法分析-开放定址散列表的实现
- 散列(开放定址法)
- 散列表之开放定址法
- (转载)散列冲突处理:开放定址法
- java 解决Hash(散列)冲突的四种方法--开放定址法(线性探测,二次探测,伪随机探测)、链地址法、再哈希、建立公共溢出区
- 开放定址法解决hash冲突问题(C语言实现)
- 开放定址散列表(线性探测法(双散列))实现文件C语言
- 散列 - 数据结构 (分离链接法、开放定址法)
- 开放定址散列表--C语言实现
- Hash函数--除留余数法--开放定址法--线性探测再散列
- 哈希表(二)(散列)开放定址法(平方)
- C实现Hash表,开放定址法
- 数据结构与算法分析-开放定址散列表的实现
- (5)散列冲突处理:开放定址法
- 开放定址散列表(线性探测法)实现文件C语言