哈希查找
来源:互联网 发布:网络教育排行榜 编辑:程序博客网 时间:2024/05/16 00:54
记录个demo吧。
建立hash表和search一定要用同一的hash算法。
#include <stdio.h>#include <stdlib.h>#define Max 6#define HashMax 5int data[Max] = {12, 160, 219, 522, 725, 9997};int HashTable[HashMax] = {0};int counter = 1;struct LinkList{int key;struct LinkList *next;};typedef struct LinkList Node;typedef Node *Link;Node HashTable_linklist[HashMax] = {0};static int hash_mod(int key){return key%HashMax;}static int collision_offset(int addr){int offset = 3;return (addr + offset)%HashMax;}static int create_hash_linklist(int key){Link pointer;Link new_node;int index;new_node = (Link) malloc(sizeof(Node));new_node->key = key;new_node->next = NULL;index = hash_mod(key);pointer = HashTable_linklist[index].next;if (pointer != NULL){new_node->next = pointer;HashTable_linklist[index].next = new_node;}elseHashTable_linklist[index].next = new_node;return 0;}int hash_search_linklist(int key){Link pointer;int index;counter = 0;index = hash_mod(key);pointer = HashTable_linklist[index].next;printf("Data[%d]: ", index);while(pointer != NULL){counter++;printf("[%d]", pointer->key);if (pointer->key == key)return 1;elsepointer = pointer->next;}return 0;}static int create_hash(int key){int hash_time = 0;int collision_time = 0;int address = hash_mod(key);int i;while (hash_time < HashMax){if (HashTable[address] == 0) /*尚未被占用*/{HashTable[address] = key;printf("Key : %d => Address %d\n", key, address);for(i = 0; i < HashMax; i++){printf("[%d]", HashTable[i]);}printf("\n");return 1;}else{collision_time++;printf("Collision %d => Address %d\n", collision_time, address);address = collision_offset(address);}hash_time++;}return 0;}/*建立hash Table 必须和Search用同样的方式*/static int hash_search(int key){int address;counter = 0;address = hash_mod(key);while(counter < HashMax){counter++;if (HashTable[address] == key)return 1;elseaddress = collision_offset(address);}return 0;}int main(int argc, char **argv){int key = 522;int index = 0;int i = 0;while(index < Max){if (create_hash(data[index]))printf("Hash Success\n");elseprintf("Hash Fulled\n");index++;}printf("Hash Table : \n");for (i = 0; i < HashMax; i++){printf("[%d]", HashTable[i]);}printf("\n");if (hash_search(key))printf("Search Time = %d\n", counter);elseprintf("Didnot find %d\n", key);counter = 0;index = 0;while(index < Max){if (create_hash_linklist(data[index]))printf("Hash Success\n");elseprintf("Hash Fulled\n");index++;}Link pointer;printf("Linklist Hash Table : \n");for (i = 0; i < HashMax; i++){printf("Table %d: ", i);pointer = HashTable_linklist[i].next;while(pointer != NULL){if (pointer->key > 0)printf("[%d]", pointer->key);pointer = pointer->next;}printf("\n");}printf("\n");printf("goint to search linklist\n");if (hash_search_linklist(key))printf("\nLinkList Search Time = %d\n", counter);elseprintf("Didnot find %d\n", key);}
0 0
- 二分查找,分块查找,哈希查找
- 顺序查找,二分查找,哈希查找
- 二分查找、分块查找、哈希查找
- 查找(二):哈希查找和索引查找
- 查找算法之哈希查找
- 查找算法之哈希查找
- 9.6 哈希查找(杂凑查找)
- 查找算法二之哈希查找
- 查找算法之哈希查找
- 查找算法之哈希查找
- 哈希查找(散列查找)
- 哈希查找
- 哈希查找
- 哈希查找
- 哈希查找
- 哈希查找算法
- 哈希查找算法
- 哈希查找
- linux 下安装 nginx
- 【郑轻】[1761]You can do it
- 迪文的DGUS屏-如何在屏幕上显示时间
- CEdit::GetLine 在Win7上返回字符串乱码问题的解决方案 CEdit::GetLine (MFC, VC++6) returns extra characters in Windows
- 子线程改变UI线程的方法
- 哈希查找
- LeetCode 8:String to Integer (atoi)
- 探究java IO之PushbackInputStream类
- VMWare 下OS X 和Ubuntu有时候不能上网问题的解决办法
- php学习日记第九棒(php 基本语法)
- 暴力英语学习法 + 严格的目标管理 = 成功快速靠谱的学好英语
- echarts之toolbox-x,y
- bzoj2127 happiness
- Ubuntu下为AndroidStudio编译并使用FFmpeg(二)AndroidStudio部分