再散列--开放定址散列表(线性探测法)加强版可再散列经优化头文件C语言

 

 


 

  散列表的东西写了有4天了.从对散列表不理解,到对散列表有了个大致的理解.从对实现不理解,到对实现有了大致的理解.今天上午写了个可再散列的一套ADT,特意优化了代码,因为在学习数据结构的同时也在学习代码的优化,于是就应用上了.这套ADT更有通用性,包括对Item类型的比较,赋值,都通过函数来完成,而且有专门的可将Item类型拆成int类型的函数,这样这套ADT用来作用到char * 类型将易于更改.付出的代价就是增加了过程调用,不过带来的益处是明显的.

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是一个简单的开放定址散列表线性探测法)的实现代码,包括头文件和实现文件: 头文件 hashtable.h: ```c #ifndef HASHTABLE_H #define HASHTABLE_H #define TABLE_SIZE 1000 typedef struct { int key; int value; } HashNode; typedef struct { HashNode *nodes; int size; } HashTable; HashTable* createHashTable(); void destroyHashTable(HashTable *table); void insert(HashTable *table, int key, int value); int get(HashTable *table, int key); #endif ``` 实现文件 hashtable.c: ```c #include <stdlib.h> #include "hashtable.h" HashTable* createHashTable() { HashTable *table = (HashTable*) malloc(sizeof(HashTable)); table->nodes = (HashNode*) calloc(TABLE_SIZE, sizeof(HashNode)); table->size = TABLE_SIZE; return table; } void destroyHashTable(HashTable *table) { free(table->nodes); free(table); } void insert(HashTable *table, int key, int value) { int hash = key % table->size; while (table->nodes[hash].value != 0) { hash = (hash + 1) % table->size; } table->nodes[hash].key = key; table->nodes[hash].value = value; } int get(HashTable *table, int key) { int hash = key % table->size; while (table->nodes[hash].value != 0) { if (table->nodes[hash].key == key) { return table->nodes[hash].value; } hash = (hash + 1) % table->size; } return 0; } ``` 在主程序中,可以使用以下方法来使用这个散列表: ```c #include <stdio.h> #include "hashtable.h" int main() { HashTable *table = createHashTable(); insert(table, 1, 100); insert(table, 2, 200); insert(table, 3, 300); printf("%d\n", get(table, 2)); destroyHashTable(table); return 0; } ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值