散列是一種用於以常數平均時間執行插入,刪除和查找的技術git
作法:將散列的同一個值保留到一張表中。以下圖:算法
1.#include <stdio.h> 2. 3.struct ListNode; 4.typedef struct ListNode *Position; 5.struct HashTbl; 6.typedef struct HashTbl *HashTable; 7. 8. 9.struct ListNode 10.{ 11. int Element; 12. Position Next; 13.}; 14. 15.typedef Position List; 16. 17.struct HashTbl { 18. int TableSize; 19. List *TheLists; 20.}; 21. 22.HashTable InitializeTable(int TableSize) { 23. HashTable H; 24. int i; 25. 26. if (TableSize < 100) 27. { 28. Error("Table size too small"); 29. return NULL; 30. } 31. //分配空間 32. H = malloc(sizeof(struct HashTbl)); 33. if (H == NULL) 34. { 35. FatalError("Out of space"); 36. } 37. //設置表大小爲素數 38. H->TableSize = NextPrime(TableSize); 39. //分配數組空間大小 40. H->TheLists = malloc(sizeof(List)*H->TableSize); 41. 42. //分配表頭 43. for (i = 0; i < H->TableSize; i++) 44. { 45. H->TheLists[i] = malloc(sizeof(struct ListNode)); 46. if (H->TheLists[i] == NULL) 47. { 48. FatalError("Out of space"); 49. } 50. else 51. { 52. H->TheLists[i]->Next = NULL; 53. } 54. 55. } 56. return H; 57.} 58. 59.//查找 60.Position Find(int Key, HashTable H) 61.{ 62. Position P; 63. List L; 64. //獲取表頭 65. L = H->TheLists[Hash(Key, H->TableSize)]; 66. P->Next; 67. while (P!=NULL&&P->Element!=Key) 68. { 69. P->Next; 70. } 71. return P; 72.} 73. 74.//插入 75.void Insert(int Key, HashTable H) { 76. Position Pos, NewCell; 77. List L; 78. Pos = Find(Key, H); 79. if (Pos==NULL) 80. { 81. NewCell = malloc(sizeof(struct ListNode)); 82. if (NewCell == NULL) { 83. 84. } 85. else 86. { 87. L = H->TheLists[Hash(Key, H->TableSize)]; 88. NewCell->Next = L->Next; 89. NewCell->Element = Key; 90. L->Next = NewCell; 91. } 92. } 93.}
1.#include <stdio.h> 2.typedef unsigned int Index; 3.typedef Index Position; 4. 5.struct HashTbl; 6.typedef struct HashTbl *HashTable; 7. 8. 9.enum KindOfEntry { Legitimate, Empty, Deleted }; 10. 11.struct HashEntry 12.{ 13. int Element; 14. enum KindOfEntry Info; 15.}; 16. 17.typedef struct HashEntry Cell; 18. 19.struct HashTbl 20.{ 21. int TableSize; 22. Cell *TheCells; 23.}; 24. 25. 26.//初始化 27.HashTable InitializeTable(int TableSize) { 28. HashTable H; 29. int i; 30. if (TableSize < 100) 31. { 32. Error(""); 33. return NULL; 34. } 35. H = malloc(sizeof(struct HashTbl)); 36. if (H == NULL) 37. { 38. FatalError(""); 39. } 40. H->TableSize = NextPrime(TableSize); 41. H->TheCells = malloc(sizeof(Cell)*H->TableSize); 42. if (H->TheCells == NULL) 43. { 44. FatalError("!!!"); 45. } 46. for (i = 0; i < H->TableSize; i++) 47. { 48. H->TheCells[i].Info = Empty; 49. } 50. return H; 51.} 52. 53. 54.Position Find(int Key, HashTable H) { 55. Position CurrentPos; 56. int CollisitionNum; 57. CollisitionNum = 0; 58. CurrentPos = Hash(Key, H->TableSize); 59. while (H->TheCells[CurrentPos].Info != NULL&&H->TheCells[CurrentPos].Element != Key) 60. { 61. CurrentPos += 2 * ++CollisitionNum - 1; 62. if (CurrentPos >= H->TableSize) 63. { 64. CurrentPos -= H->TableSize; 65. } 66. } 67. return CurrentPos; 68.} 69. 70. 71.void Insert(int Key, HashTable H) 72.{ 73. Position Pos; 74. Pos = Find(Key, H); 75. if (H->TheCells[Pos].Info=Legitimate) 76. { 77. H->TheCells[Pos].Info = Legitimate; 78. H->TheCells[Pos].Element = Key; 79. } 80.} 81. 82.//再散列 83.HashTable Rehash(HashTable H) 84.{ 85. int i, OldSize; 86. Cell *OldCells; 87. 88. OldCells = H->TheCells; 89. OldSize = H->TableSize; 90. 91. 92. H = InitializeTable(2 * OldSize); 93. 94. for (i=0; i < OldSize; i++) 95. { 96. if (OldCells[i].Info == Legitimate) 97. { 98. Insert(OldCells[i].Element, H); 99. } 100. } 101. free(OldCells); 102. return H; 103.}
和B-樹相似。數組