散列的冲突解决方法之分离链接法
2014-03-31 09:33
369 查看
分离链接法(separate chaining):将散列到同一个值的所有元素保留到一个表中(用指针实现的单链表)。
/* 实现分离链接法所需要的类型声明 */ #ifndef _HashSep_H #define _HahsSep_H struct ListNode; typedef struct ListNode *Position; struct HashTbl; typedef struct HashTbl *HashTable; HashTable InitializeTable( int TableSize ); void DestroyTable( HashTable H ); Position Find( ElementType Key, HashTable H ); void Insert( ElementType Key, HashTable H ); ElementType Retrieve( Position P ); /* Routines such as Delete and MakeEmpty are omitted */ #endif /* _HashSep_H */ /* Place in the implementation file */ struct ListNode { ElementType Element; Position Next; }; typedef Position List; /* List *TheList will be an array of lists, allocated later */ /* The lists use headers(for simplicity */ /* though this wastes space */ struct HashTbl { int TableSize; List *TheLists; };
/* 分离链接散列表的初始化例程 */ HashTable InitializeTable( int TableSize ) { HashTable H; int i; if( TableSize < MinTableSize ) { Error( "Table size too small" ); return NULL; } /* Allocate table */ H = malloc( sizeof( struct HashTbl )); if( H == NULL ) FatalError( "Out of space!!!" ); H->TableSize = NextPirme( TableSize ); /* Allocate array of lists */ H->TheLists = malloc( sizeof( List ) * H->TableSize ); if( H->Thelists == NULL ) FatalError( "Out of space!!!" ); /* Allocate list headers */ for( i = 0; i < H->TableSize; i++) { H->TheLists[ i ] = malloc( sizeof( struct ListNode ) ); if( H->TheLists[ i ] == NULL) FatalError( "Out of space!!!" ); else H->TheLists[ i ]->Next = NULL; } return H; }
/* 分离链接散列表的Find例程 */ Position Find( ElementType Key, HashTable H ) { Position P; List L; L = H->TheLists[ Hash( Key, H->TableSize ) ]; P = L->Next; while( P != NULL && P->Element != Key ) /* Probably need strcmp!! */ P = P->Next; return P; }
/* 分离链接散列表的Insert例程 */ void Insert( ElementType Key, HashTable H ) { Position Pos, NewCell; List L; Pos = Find( Key, H ); if( Pos == NULL ) /* Key is not found */ { NewCell = malloc( sizeof( struct ListNode ) ); if( NewCell == NULL ) FatalError( "Out of space!!!" ); else { L = H->TheLists[ Hash( Key, H->TableSize ) ]; NewCell->Next = L->Next; NewCell->Element = Key; /* Probably need strcpy ! */ L->Next = NewCell; } } }
相关文章推荐
- HashTable 解决碰撞(冲突)的方法 —— 分离链接法(separate chaining)
- 哈希表冲突的两个解决方法线性探测和分离链接法
- 数据结构--解决散列冲突,分离链接法
- 数据结构--解决散列冲突,分离链接法
- java 解决Hash(散列)冲突的四种方法--开放定址法(线性探测,二次探测,伪随机探测)、链地址法、再哈希、建立公共溢出区
- [转载]CSS<a href>链接样式冲突解决方法!
- Hash 散列表(分离链接法解决冲突)
- java 解决Hash(散列)冲突的四种方法--开放定址法(线性探测,二次探测,伪随机探测)、链地址法、再哈希、建立公共溢出区
- 【散列表-链接法解决冲突】利用链接法来解决冲突的散列表
- 散列表冲突解决方法11.1.3
- 算法学习 - Hash Table操作,分离链接法解决哈希冲突
- 哈希表分离链接发解决冲突
- 数据结构——采用分离链接解决冲突问题的散列表
- 链接技术解决冲突的散列表
- C++与C函数的相互调用方法以及error C2732:链接规范与"f"的早期规范冲突的解决办法
- 散列查找--解决冲突的方法
- 散列表-链接法解决冲突
- 数据结构之散列表冲突的解决方法
- 解决散列表冲突问题-分离链接散列表
- 采用线性探测方法解决冲突