手动数字哈希表-C语言

7 篇文章 0 订阅
3 篇文章 0 订阅

手动实现哈希表

哈希表的定义

哈希表的定义:

  • 哈希函数:将任意长度的输入(一般是字符串)映射到一个固定长度的输出(一般是整数)上,这个映射函数称为哈希函数。
  • 哈希表:是一种数据结构,它以某种方式将键(key)映射到值(value)上。
  • 哈希表的特点:
    • 哈希表的存储空间是有限的,当元素的数量超过了存储空间时,就会出现哈希冲突。
    • 哈希表的查找、插入、删除操作的时间复杂度都为O(1)。

手动实现哈希表

手动实现哈希表的步骤如下:

  1. 定义哈希表的结构,包括哈希表的大小,以及哈希表的数组。
  2. 定义哈希函数,将键映射到数组的索引上。
  3. 定义节点的结构,包括节点的键和值,以及指向下一个节点的指针。
  4. 定义插入、查找、删除操作。
  5. 测试哈希表。

下面是实现的代码:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
// 手动实现哈希表

// 哈希表长定义
#define HASH_SIZE 1000001

// 哈希函数
#define HASH(x) ((x) % HASH_SIZE)

// 节点的定义
struct Node {
    int key;
    int value;
    struct Node *next;
};

// 哈希表的结构
struct HashTable {
    struct Node *table[HASH_SIZE];
};

// 创建哈希表
struct HashTable *createHashTable() {
    struct HashTable *ht = (struct HashTable *)malloc(sizeof(struct HashTable));
    memset(ht->table, 0, sizeof(ht->table));
    return ht;
}

// 插入元素
void insert(struct HashTable *ht, int key, int value) {
    // 先查找是否存在该元素
    int index = HASH(key);
    // 遍历链表
    struct Node *p = ht->table[index];
    while (p) {
        if (p->key == key) {
            p->value = value;
            return;
        }
        p = p->next;
    }
    // 元素不存在,插入到链表头
    struct Node *newNode = (struct Node *)malloc(sizeof(struct Node));
    newNode->key = key;
    newNode->value = value;
    newNode->next = ht->table[index];
    ht->table[index] = newNode;
}

// 查找元素
int search(struct HashTable *ht, int key) {
    int index = HASH(key);
    struct Node *p = ht->table[index];
    while (p) {
        if (p->key == key) {
            return p->value;
        }
        p = p->next;
    }
    return -1;
}

// 删除元素
void delete(struct HashTable *ht, int key) {
    int index = HASH(key);
    struct Node *p = ht->table[index];
    struct Node *q = NULL;
    while (p) {
        if (p->key == key) {
            if (q) {
                q->next = p->next;
            } else {
                ht->table[index] = p->next;
            }
            free(p);
            return;
        }
        q = p;
        p = p->next;
    }
}

// 测试代码
int main() {
    // 测试哈希表
    struct HashTable *ht = createHashTable();
    // 插入元素
    insert(ht, 1, 10);
    insert(ht, 2, 20);
    insert(ht, 3, 30);
    // 查找元素
    printf("%d\n", search(ht, 2)); // 20
    // 删除元素
    delete(ht, 2);
    // 查找元素
    printf("%d\n", search(ht, 2)); // -1
    return 0;
}

哈希冲突

当多个元素映射到同一个索引时,称为哈希冲突。常见的哈希冲突解决方法有开放寻址法、链表法、再散列法。

开放寻址法

开放寻址法是指当发生哈希冲突时,重新探测一个空闲位置,直到找到一个空闲位置为止。

void insert(struct HashTable *ht, int key, int value) {
    int index = HASH(key);
    int i = 0;
    while (ht->table[index] != NULL) {
        // 探测下一个位置
        index = (index + i) % HASH_SIZE;
        i++;
    }
    // 找到空闲位置
    struct Node *newNode = (struct Node *)malloc(sizeof(struct Node));
    newNode->key = key;
    newNode->value = value;
    newNode->next = ht->table[index];
    ht->table[index] = newNode;
}

链表法

链表法是指将哈希表的数组元素指向链表,每个链表中存储同一哈希值的所有元素。

struct Node {
    int key;
    int value;
    struct Node *next;
};

struct HashTable {
    struct Node **table;
};

void insert(struct HashTable *ht, int key, int value) {
    int index = HASH(key);
    struct Node *newNode = (struct Node *)malloc(sizeof(struct Node));
    newNode->key = key;
    newNode->value = value;
    newNode->next = ht->table[index];
    ht->table[index] = newNode;
}

int search(struct HashTable *ht, int key) {
    int index = HASH(key);
    struct Node *p = ht->table[index];
    while (p) {
        if (p->key == key) {
            return p->value;
        }
        p = p->next;
    }
    return -1;
}

void delete(struct HashTable *ht, int key) {
    int index = HASH(key);
    struct Node *p = ht->table[index];
    struct Node *q = NULL;
    while (p) {
        if (p->key == key) {
            if (q) {
                q->next = p->next;
            } else {
                ht->table[index] = p->next;
            }
            free(p);
            return;
        }
        q = p;
        p = p->next;
    }
}

struct HashTable *createHashTable() {
    struct HashTable *ht = (struct HashTable *)malloc(sizeof(struct HashTable));
    ht->table = (struct Node **)malloc(sizeof(struct Node *) * HASH_SIZE);
    memset(ht->table, 0, sizeof(ht->table));
    return ht;
}

再散列法

再散列法是指当发生哈希冲突时,重新计算哈希值,直到找到一个空闲位置为止。

void insert(struct HashTable *ht, int key, int value) {
    int index = HASH(key);
    while (ht->table[index] != NULL) {
        // 重新计算哈希值
        key = (key + 1) % HASH_SIZE;
        index = HASH(key);
    }
    // 找到空闲位置
    struct Node *newNode = (struct Node *)malloc(sizeof(struct Node));
    newNode->key = key;
    newNode->value = value;
    newNode->next = ht->table[index];
    ht->table[index] = newNode;
}

哈希表的应用

  • 字符串查找(涉及到字符串哈希):字符串查找是哈希表的典型应用。
  • 缓存:缓存是哈希表的另一个典型应用。
  • 数据库索引:数据库索引也是哈希表的一种应用。
  • 哈希函数的设计:哈希函数的设计可以影响哈希表的性能。
  • 哈希表的扩展:哈希表的扩展可以实现动态哈希表。

参考






每一个不曾起舞的日子,都是对生命的辜负。
  • 3
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
以下是一个简单的哈希表设计 C 语言代码实现,仅供参考: ```c #include <stdio.h> #include <stdlib.h> #include <string.h> #define SIZE 100 typedef struct { char* key; int value; } HashNode; typedef struct { HashNode** nodes; int size; } HashTable; HashTable* createHashTable(int size) { HashTable* hashtable = (HashTable*)malloc(sizeof(HashTable)); hashtable->nodes = (HashNode**)calloc(size, sizeof(HashNode*)); hashtable->size = size; return hashtable; } int hash(char* key, int size) { int hash = 0; int len = strlen(key); for (int i = 0; i < len; i++) { hash = (hash * 31 + key[i]) % size; } return hash; } void put(HashTable* hashtable, char* key, int value) { int hashValue = hash(key, hashtable->size); HashNode* node = hashtable->nodes[hashValue]; while (node != NULL) { if (strcmp(node->key, key) == 0) { node->value = value; return; } node = node->next; } node = (HashNode*)malloc(sizeof(HashNode)); node->key = key; node->value = value; node->next = hashtable->nodes[hashValue]; hashtable->nodes[hashValue] = node; } int get(HashTable* hashtable, char* key) { int hashValue = hash(key, hashtable->size); HashNode* node = hashtable->nodes[hashValue]; while (node != NULL) { if (strcmp(node->key, key) == 0) { return node->value; } node = node->next; } printf("Error: Key not found\n"); return -1; } void delete(HashTable* hashtable, char* key) { int hashValue = hash(key, hashtable->size); HashNode* node = hashtable->nodes[hashValue]; HashNode* prev = NULL; while (node != NULL) { if (strcmp(node->key, key) == 0) { if (prev == NULL) { hashtable->nodes[hashValue] = node->next; } else { prev->next = node->next; } free(node); return; } prev = node; node = node->next; } printf("Error: Key not found\n"); } void printHashTable(HashTable* hashtable) { for (int i = 0; i < hashtable->size; i++) { printf("%d: ", i); HashNode* node = hashtable->nodes[i]; while (node != NULL) { printf("(%s, %d) ", node->key, node->value); node = node->next; } printf("\n"); } } int main() { HashTable* hashtable = createHashTable(SIZE); put(hashtable, "apple", 3); put(hashtable, "banana", 2); put(hashtable, "orange", 1); printHashTable(hashtable); printf("Get value of apple: %d\n", get(hashtable, "apple")); delete(hashtable, "banana"); printHashTable(hashtable); return 0; } ``` 解释一下上述代码: - `HashTable` 结构体包含一个 `nodes` 数组,用来存储哈希表中的节点,以及 `size` 表示哈希表的大小。 - `HashNode` 结构体包含一个 `key` 字符串和一个 `value` 整数,表示键值对。 - `createHashTable` 函数创建一个哈希表,并返回其指针。 - `hash` 函数接收一个键和哈希表的大小,计算该键的哈希值,并返回对应哈希表中的位置。 - `put` 函数接收键值对,将其插入哈希表中。如果该键已经存在,则更新对应的值。 - `get` 函数接收一个键,返回对应的值。如果该键不存在,则输出错误信息,并返回 -1。 - `delete` 函数接收一个键,将其从哈希表中删除。如果该键不存在,则输出错误信息。 - `printHashTable` 函数打印哈希表的内容,用于调试。 - `main` 函数创建一个哈希表,插入若干键值对,然后调用各种函数测试其功能。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

_廿_尘

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值