摘要: 哈希表是一种神奇、实用、粗暴的方法. 其核心思想是空间换时间.
1. 代码
先上代码, 再说废话.
/**
* Hash table.
*
* @author Fan Min minfanphd@163.com.
*/
#include <stdio.h>
#include <malloc.h>
#define TABLE_SIZE 19
/**
* <key, value> pair.
*/
typedef struct Node{
int key;
char value;
}Node, *NodePtr;
/**
* <key, value> pair.
*/
typedef struct HashList{
int length;
NodePtr elements;
}HashList, *ListPtr;
/**
* Initialize a data array.
*/
ListPtr initList(int* paraKeys, char* paraValues, int paraLength){
int i, tempPosition;
ListPtr resultPtr = (ListPtr)malloc(sizeof(struct HashList));
// Step 1. Initialize.
resultPtr->length = paraLength;
resultPtr->elements = (NodePtr)malloc(TABLE_SIZE * sizeof(struct Node));
for (i = 0; i < TABLE_SIZE; i ++){
resultPtr->elements[i].key = -1;
resultPtr->elements[i].value = 'x';
}//Of for i
// Step 2. Fill the data.
for (i = 0; i < paraLength; i++) {
// Hash.
tempPosition = paraKeys[i] % TABLE_SIZE;
// Find an empty position
while (resultPtr->elements[tempPosition].key != -1) {
tempPosition = (tempPosition + 1) % TABLE_SIZE;
printf("Collision, move forward for key %d.\r\n", paraKeys[i]);
} // Of while
// printf("Inserting %d to %d\r\n", paraKeys[i], tempPosition);
resultPtr->elements[tempPosition].key = paraKeys[i];
resultPtr->elements[tempPosition].value = paraValues[i];
} // Of for i
return resultPtr;
}//Of initList
/**
* Hash search.
*
* @param paraKey The given key.
* @return The content of the key.
*/
char hashSearch(ListPtr paraPtr, int paraKey) {
int tempPosition = paraKey % TABLE_SIZE;
// printf("For %d\r\n", paraKey);
// printf("Looking at position %d\r\n", tempPosition);
while (paraPtr->elements[tempPosition].key != -1) {
if (paraPtr->elements[tempPosition].key == paraKey) {
// printf("Found\r\n");
return paraPtr->elements[tempPosition].value;
} // Of if
// printf("Not this one for %d.\r\n", paraKey);
tempPosition = (tempPosition + 1) % TABLE_SIZE;
// printf("Looking at position %d\r\n", tempPosition);
} // Of while
return 'x';
}// Of hashSearch
void hashSearchTest() {
int tempUnsortedKeys[] = { 16, 33, 38, 69, 57, 95, 86 };
char tempContents[] = { 'h', 'e', 'l', 'o', 'w', 'r', 'd' };
ListPtr tempPtr = initList(tempUnsortedKeys, tempContents, 7);
printf("Search result of 95 is: %c\r\n", hashSearch(tempPtr, 95));
printf("Search result of 38 is: %c\r\n", hashSearch(tempPtr, 38));
printf("Search result of 57 is: %c\r\n", hashSearch(tempPtr, 57));
printf("Search result of 4 is: %c\r\n", hashSearch(tempPtr, 4));
}// Of hashSearchTest
/**
* The entrance of the program.
*/
int main() {
hashSearchTest();
return 1;
}// Of main
2. 运行结果
Collision, move forward for key 57.
Collision, move forward for key 95.
Collision, move forward for key 95.
Search result of 95 is: r
Search result of 38 is: l
Search result of 57 is: w
Search result of 4 is: x
Press any key to continue
3. 代码说明
- 神奇、实用、粗暴的方法. 空间换时间.
- 保证空间足够.
- 在构造方法中装入数据. 自己可以写代码增加数据.
- 使用 (最简单的) 除数取余法获得数据存放地址 (下标).
- 使用 (最简单的) 顺移位置法解决冲突.
- 搜索的时间复杂度仅与冲突概率相关, 间接地就与装填因子相关.
- 如果空间很多, 可以看出时间复杂度为 O ( 1 ) O(1) O(1).
- 注释掉了一些调试语句.