链式哈希表的实现
来源:互联网 发布:mrtg python 编辑:程序博客网 时间:2024/06/07 04:51
哈希算法一般用于快速查找和加密算法
chtbl.h
#ifndef CHTBL_H#define CHTBL_H#include <stdlib.h>#include "list.h"/* Define a structure for chained hash tables. */typedef struct CHTbl_ {int buckets;int (*h)(const void *key);int (*match)(const void *key1, const void *key2);void (*destroy)(void *data);int size;List *table;} CHTbl;/* Public Interface. */int chtbl_init(CHTbl *htbl, int buckets, int (*h)(const void *key), int (*match)(const void *key1, const void *key2), void (*destroy)(void *data));void chtbl_destroy(CHTbl *htbl);int chtbl_insert(CHTbl *htbl, const void *data);int chtbl_remove(CHTbl *htbl, void **data);int chtbl_lookup(const CHTbl *htbl, void **data);#define chtbl_size(htbl) ((htbl)->size)#endif
chtbl.m
#include <stdlib.h>#include <string.h>#include "list.h"#include "chtbl.h"/* chtbl_init */int chtbl_init(CHTbl *htbl, int buckets, int (*h)(const void *key), int (*match)(const void *key1, const void *key2), void (*destroy)(void*data)) {int i;/*Allocate space for the hash table.*/if ((htbl->table = (List *)malloc(buckets * sizeof(List))) == NULL) return -1;/*Initialize the buckets.*/htbl->buckets = buckets;for (i = 0; i < htbl->buckets; i++) list_init(&htbl->table[i], destroy);/*Encapsulate the functions.*/htbl->h = h;htbl->match = match;htbl->destroy = destroy;/*Initialize the number of elements in the table.*/htbl->size = 0;return 0;}/* chtbl_destroy */void chtbl_destroy(CHTbl *htbl) {int i;/*Destroy each bucket.*/for (i = 0; i < htbl->buckets; i++) { list_destroy(&htbl->table[i]);}/*Free the storage allocated for the hash table.*/free(htbl->table);/*No operations are allowed now, but clear the structure as a precaution.*/memset(htbl, 0, sizeof(CHTbl));return;}/* chtbl_insert */int chtbl_insert(CHTbl *htbl, const void *data) {void *temp;int bucket, retval;/* Do nothing if the data is already in the table. */temp = (void *)data;if (chtbl_lookup(htbl, &temp) == 0) return 1;/* Hash the key. */bucket = htbl->h(data) % htbl->buckets;/*Insert the data into the bucket.*/if ((retval = list_ins_next(&htbl->table[bucket], NULL, data)) == 0) htbl->size++;return retval;}/* chtbl_remove */int chtbl_remove(CHTbl *htbl, void **data) {ListElmt *element, *prev;int bucket;/*Hash the key.*/bucket = htbl->h(*data) % htbl->buckets;/*Search for the data in the bucket.*/prev = NULL;for (element = list_head(&htbl->table[bucket]); element != NULL; element = list_next(element)) { if (htbl->match(*data, list_data(element))) { /*Remove the data from the bucket.*/ if (list_rem_next(&htbl->table[bucket], prev, data) == 0) { htbl->size--; return 0; } else { return -1; } } prev = element;}/*Return that the data was not found.*/return -1;}/* chtbl_lookup */int chtbl_lookup(const CHTbl *htbl, void **data) {ListElmt *element;int bucket;/*Hash the key.*/bucket = htbl->h(*data) % htbl->buckets;/*Search for the data in the bucket.*/for (element = list_head(&htbl->table[bucket]); element != NULL; element = list_next(element)) { if (htbl->match(*data, list_data(element))) { /*Pass back the data from the table.*/ *data = list_data(element); return 0; }}/*Return that the data was not found.*/return -1;}
0 0
- 链式哈希表的实现
- 链式实现的堆栈
- 链式表的实现
- 栈的链式实现
- 链式栈的实现
- 链式链的实现
- 链式队列的实现
- 链式队列的实现
- 队的链式实现
- 栈的链式实现
- 链式队列的实现
- 链式队列的实现。
- 链式队列的实现
- 链式队列的实现
- 链式队列的实现
- 队列的链式实现
- 链式栈的实现
- 链式队列的实现
- 模式识别十--k-均值聚类算法的研究与实现
- 数据结构之——快速排序
- what is AttributeOverride annotation used for in Hibernate
- LintCode: 摊平嵌套的列表
- QT写的象棋(C++)
- 链式哈希表的实现
- 深度学习主机环境配置: Ubuntu16.04+Nvidia GTX 1080+CUDA8.0
- POJ_3169 Layout(差分约束系统-最短路)
- 非比较排序-----计数排序,基数排序。
- 【2】CRM_用户模块_注册功能
- 给定一个树的节点ID,递归遍历出以这个节点为根的子树,以JSON格式返回
- mac优雅的软件管理工具homebrew
- 模式识别十一--分类回归决策树CART的研究与实现
- 记数据结构简单课设之无向图的简单路径