c语言hashtable,c语言实现的hashtable分享

头文件 hashtable.h

复制代码 代码如下:

typedef struct _Bucket

{

char *key;

void *value;

struct _Bucket *next;

} Bucket;

typedef struct _HashTable

{

int size;

int total;

struct _Bucket *buckets;

} HashTable;

int hash_init(HashTable **ht);

int hash_find(HashTable *ht, char *key, void **result);

int hash_insert(HashTable *ht, char *key, void *value);

int hash_remove(HashTable *ht, char *key);

int hash_loop(HashTable *ht, void **result);

//int hash_index(HashTable *ht, char *key);

static unsigned int ELFHash(char *str, unsigned int length);

hashtable.c

复制代码 代码如下:

#include

#include

#include

#include "hashtable.h"

#include "mempool.h"

#include "log.h"

#define SUCCESS 1

#define FAILED 0

#define HASH_LEN 5

int hash_init(HashTable **ht) {

(*ht) = (HashTable *)malloc(sizeof(HashTable));

if (NULL == ht) {

write_log("HashTable init error");

exit(1);

}

(*ht)->size = 0;

(*ht)->total = HASH_LEN;

Bucket *bucket = (Bucket *)malloc(sizeof(Bucket) * HASH_LEN);

memset(bucket, 0, sizeof(sizeof(Bucket) * HASH_LEN));

(*ht)->buckets = bucket;

return SUCCESS;

}

int hash_insert(HashTable *ht, char *key, void *value) {

if (ht->size >= ht->total) {

ht->buckets = (Bucket *)realloc(ht->buckets, sizeof(Bucket) * (ht->size + HASH_LEN));

ht->total = ht->size + HASH_LEN;

}

int index = hash_index(ht, key);

Bucket *bucket = &ht->buckets[index];

int _tmpindex;

char _tmpindexstr[20];

while (NULL != bucket->value) {

while (NULL != bucket->next) {

if (strcmp(key, bucket->key) == 0) {

memset(bucket->value, 0, sizeof(bucket->value));

memcpy(bucket->value, value, sizeof(value));

return SUCCESS;

}

bucket = bucket->next;

}

do {

_tmpindex = abs(rand() - index);

sprintf(_tmpindexstr, "%d", _tmpindex);

_tmpindex = hash_index(ht, _tmpindexstr);

} while (_tmpindex == index || ht->buckets[_tmpindex].value != NULL);

index = _tmpindex;

bucket->next = &ht->buckets[index];

bucket = bucket->next;

}

bucket->key = (char *)malloc(sizeof(key));

bucket->value = (void *)malloc(sizeof(value));

memcpy(bucket->key, key, sizeof(key));

memcpy(bucket->value, value, sizeof(value));

bucket->next = NULL;

ht->size ++;

return SUCCESS;

}

int hash_find(HashTable *ht, char *key, void **result) {

int index = hash_index(ht, key);

Bucket *bucket = &ht->buckets[index];

if (NULL == bucket->value) {

return FAILED;

}

while (strcmp(key, bucket->key)) {

if (NULL != bucket->next) {

bucket = bucket->next;

} else {

break;

}

}

if (NULL == bucket->value || strcmp(key, bucket->key)) {

return FAILED;

}

*result = bucket->value;

return SUCCESS;

}

int hash_delete(HashTable *ht, char *key) {

int index = hash_index(ht, key);

Bucket *bucket = &ht->buckets[index];

if (NULL == bucket->value) {

return FAILED;

}

while (strcmp(key, bucket->key)) {

if (NULL != bucket->next) {

bucket = bucket->next;

} else {

break;

}

}

if (NULL == bucket->value || strcmp(key, bucket->key)) {

return FAILED;

}

memset(bucket, 0, sizeof(Bucket));

ht->size --;

return SUCCESS;

}

void hash_status(HashTable *ht) {

printf("Total Size:\t\t%d\n", ht->total);

printf("Current Size:\t\t%d\n", ht->size);

}

int hash_index(HashTable *ht, char *key) {

return ELFHash(key, ht->total);

}

// ELF Hash Function

static unsigned int ELFHash(char *str, unsigned int length){

unsigned int hash = 0;

unsigned int x = 0;

while (*str)

{

hash = (hash << 4) + (*str++);//hash左移4位,把当前字符ASCII存入hash低四位。

if ((x = hash & 0xF0000000L) != 0)

{

//如果最高的四位不为0,则说明字符多余7个,现在正在存第8个字符,如果不处理,再加下一个字符时,第一个字符会被移出,因此要有如下处理。

//该处理,如果对于字符串(a-z 或者A-Z)就会仅仅影响5-8位,否则会影响5-31位,因为C语言使用的算数移位

//因为1-4位刚刚存储了新加入到字符,所以不能>>28

hash ^= (x >> 24);

//上面这行代码并不会对X有影响,本身X和hash的高4位相同,下面这行代码&~即对28-31(高4位)位清零。

hash &= ~x;

}

}

//返回一个符号位为0的数,即丢弃最高位,以免函数外产生影响。(我们可以考虑,如果只有字符,符号位不可能为负)

return (hash & 0x7FFFFFFF) % length;

}

其中key的映射使用的是 ELFHash 算法

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
智慧校园整体解决方案是响应国家教育信息化政策,结合教育改革和技术创新的产物。该方案以物联网、大数据、人工智能和移动互联技术为基础,旨在打造一个安全、高效、互动且环保的教育环境。方案强调从数字化校园向智慧校园的转变,通过自动数据采集、智能分析和按需服务,实现校园业务的智能化管理。 方案的总体设计原则包括应用至上、分层设计和互联互通,确保系统能够满足不同用户角色的需求,并实现数据和资源的整合与共享。框架设计涵盖了校园安全、管理、教学、环境等多个方面,构建了一个全面的校园应用生态系统。这包括智慧安全系统、校园身份识别、智能排课及选课系统、智慧学习系统、精品录播教室方案等,以支持个性化学习和教学评估。 建设内容突出了智慧安全和智慧管理的重要性。智慧安全管理通过分布式录播系统和紧急预案一键启动功能,增强校园安全预警和事件响应能力。智慧管理系统则利用物联网技术,实现人员和设备的智能管理,提高校园运营效率。 智慧教学部分,方案提供了智慧学习系统和精品录播教室方案,支持专业级学习硬件和智能化网络管理,促进个性化学习和教学资源的高效利用。同时,教学质量评估中心和资源应用平台的建设,旨在提升教学评估的科学性和教育资源的共享性。 智慧环境建设则侧重于基于物联网的设备管理,通过智慧教室管理系统实现教室环境的智能控制和能效管理,打造绿色、节能的校园环境。电子班牌和校园信息发布系统的建设,将作为智慧校园的核心和入口,提供教务、一卡通、图书馆等系统的集成信息。 总体而言,智慧校园整体解决方案通过集成先进技术,不仅提升了校园的信息化水平,而且优化了教学和管理流程,为学生、教师和家长提供了更加便捷、个性化的教育体验。
当然,我可以为您编写一个简单的 HashTable 示例程序,实现基本的哈希表功能。以下是一个基本的实现示例: ``` #include <stdio.h> #include <stdlib.h> #define SIZE 10 struct Node { int data; int key; struct Node* next; }; struct Node* hashTable[SIZE]; void insert(int key, int data) { struct Node* new_node = (struct Node*) malloc(sizeof(struct Node)); new_node->key = key; new_node->data = data; new_node->next = NULL; int hash_index = key % SIZE; if (hashTable[hash_index] == NULL){ hashTable[hash_index] = new_node; }else { struct Node* tmp = hashTable[hash_index]; while (tmp->next != NULL) { tmp = tmp->next; } tmp->next = new_node; } } void delete(int key) { int hash_index = key % SIZE; struct Node* head = hashTable[hash_index]; struct Node* tmp = head; struct Node* prev = NULL; if (head == NULL) { printf("Key not found\n"); return; } while (tmp->next != NULL && tmp->key != key) { prev = tmp; tmp = tmp->next; } if (tmp->key == key) { if (prev == NULL) { hashTable[hash_index] = tmp->next; } else { prev->next = tmp->next; } free(tmp); printf("Key deleted\n"); } else { printf("Key not found\n"); } } void search(int key) { int hash_index = key % SIZE; struct Node* head = hashTable[hash_index]; struct Node* tmp = head; if (head == NULL) { printf("Key not found\n"); return; } while (tmp != NULL && tmp->key != key) { tmp = tmp->next; } if (tmp == NULL) { printf("Key not found\n"); } else { printf("Key found, value = %d\n", tmp->data); } } void display() { int i; for (i = 0; i < SIZE; i++) { struct Node* tmp = hashTable[i]; printf("Slot[%d]: ", i); while (tmp != NULL) { printf("%d:%d -> ", tmp->key, tmp->data); tmp = tmp->next; } printf("NULL\n"); } } int main() { int choice, key, data; while(1) { printf("\n"); printf("1.Insert element into the table\n"); printf("2.Delete element from the table\n"); printf("3.Search element from the table\n"); printf("4.Display all elements of the table\n"); printf("5.Exit\n"); printf("Enter your choice: "); scanf("%d", &choice); switch(choice) { case 1: printf("Enter key: "); scanf("%d", &key); printf("Enter data: "); scanf("%d", &data); insert(key, data); break; case 2: printf("Enter the key to delete: "); scanf("%d", &key); delete(key); break; case 3: printf("Enter the key to search: "); scanf("%d", &key); search(key); break; case 4: display(); break; case 5: exit(0); default: printf("Invalid choice\n"); } } return 0; } ``` 这个示例程序实现了哈希表的基础功能,包括插入元素、删除元素、搜索元素和显示哈希表内容。使用时,只需要创建一个哈希表数组,然后通过对键值进行哈希计算,在哈希表数组中寻找对应的位置,并在该位置上插入、搜索和删除数据即可。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值