HashTable 解决碰撞(冲突)的方法 —— 分离链接法(separate chaining)
                                                            生活随笔
收集整理的這篇文章主要介紹了
                                HashTable 解决碰撞(冲突)的方法 —— 分离链接法(separate chaining)
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.                        
                                1. ListNode 及 HashTable 的類型聲明
- 聲明 typedef int ElementType; typedef unsigned int Index;struct ListNode; typedef struct ListNode* Position; struct HashTbl; typedef struct HashTbl* HashTable;HashTable InitHashTable(int TableSize); void DestroyHashTable(HashTable H); Position Find(ElementType Element, HashTable H); void Insert(ElementType Element, HashTable H);; ElementType Retrieve(Position P);
- 定義 struct ListNode{ElementType Element;Position Next; };typedef Position List;struct HashTbl {int TableSize;List* TheLists; };
2. HashTable 的構建
HashTable InitHashTable(int TableSize){HashTable H;if (TableSize < MinTableSize){Error(" ... ");return NULL;}H = (HashTable)malloc(sizeof(struct HashTbl));if (!H)FatalError("out of space !!!");H->TableSize = NextPrime(TableSize);H->TheLists = (List*)malloc(sizeof(struct ListNode)*H->TableSize);for (int i = 0; i < H->TableSize; ++i){H->TheLists[i] = (List)malloc(sizeof(struct ListNode));if (!H->TheLists[i])FatalError("");elseH->TheLists[i]->Next = NULL;} }3. 插入新的結點
void Insert(ElementType Key, HashTable H){Position Pos, NewCell;List L;Pos = Find(key, H);if (!Pos){NewCell = (Position)malloc(sizeof(struct ListNode));if (!NewCell)FatalError("");L = H->TheLists[Hash(Key, H->TableSize)];NewCell->Next = L->Next;// 插入在首部NewCell->Element = Key;L->Next = NewCell->Next;} }轉載于:https://www.cnblogs.com/mtcnn/p/9423656.html
總結
以上是生活随笔為你收集整理的HashTable 解决碰撞(冲突)的方法 —— 分离链接法(separate chaining)的全部內容,希望文章能夠幫你解決所遇到的問題。
 
                            
                        - 上一篇: iOS实现自定义的弹出视图(popVie
- 下一篇: 【jQuery】parent()和par
