数据结构--哈希表

哈希表是一种用于实现高效查找的数据结构。它通过将键值对存储在一个数组中,并使用哈希函数将键映射到数组的索引,从而实现快速查找、插入和删除操作。本文将介绍如何在C语言中实现哈希表并进行键值对的查找。

哈希表的基本概念

哈希表由以下几个部分组成:

  1. 哈希函数:将键映射到哈希表的索引位置。
  2. 冲突解决方法:当两个不同的键映射到同一索引位置时,需要解决冲突。常见的方法有链地址法和开放地址法。
  3. 键值对:存储在哈希表中的数据。
键值对和哈希表的定义

首先,我们定义键值对和哈希表的数据结构。

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#define TABLE_SIZE 100 // 哈希表的大小
#define KEY_SIZE 50  // 键的最大长度
#define VALUE_SIZE 100 // 值的最大长度

typedef struct KeyValuePair {
    char key[KEY_SIZE];
    char value[VALUE_SIZE];
    struct KeyValuePair *next; // 指向下一个节点,处理冲突
} KeyValuePair;

typedef struct {
    KeyValuePair *table[TABLE_SIZE]; // 哈希表数组
} HashTable;

// 初始化哈希表
void initHashTable(HashTable *hashTable) {
    for (int i = 0; i < TABLE_SIZE; i++) {
        hashTable->table[i] = NULL;
    }
}
哈希函数的实现

我们需要一个哈希函数将键映射到哈希表的索引位置。这里使用一个简单的哈希函数。

// 哈希函数
unsigned int hash(const char *key) {
    unsigned int hashValue = 0;
    while (*key) {
        hashValue = (hashValue << 5) + *key++;
    }
    return hashValue % TABLE_SIZE;
}
插入键值对的实现

接下来,我们实现插入键值对的函数。

// 向哈希表中插入键值对
void insertKeyValuePair(HashTable *hashTable, const char *key, const char *value) {
    unsigned int index = hash(key);
    KeyValuePair *newPair = (KeyValuePair *)malloc(sizeof(KeyValuePair));
    strcpy(newPair->key, key);
    strcpy(newPair->value, value);
    newPair->next = hashTable->table[index];
    hashTable->table[index] = newPair;
}
查找键值对的实现

然后,我们实现查找键值对的函数。

// 查找键值对
const char* searchKeyValuePair(HashTable *hashTable, const char *key) {
    unsigned int index = hash(key);
    KeyValuePair *pair = hashTable->table[index];
    while (pair != NULL) {
        if (strcmp(pair->key, key) == 0) {
            return pair->value; // 返回找到的值
        }
        pair = pair->next;
    }
    return NULL; // 查找失败
}
测试函数

最后,我们编写一个测试函数来验证上述哈希表的实现。

int main() {
    HashTable hashTable;
    initHashTable(&hashTable);

    // 插入一些键值对
    insertKeyValuePair(&hashTable, "name", "Alice");
    insertKeyValuePair(&hashTable, "age", "30");
    insertKeyValuePair(&hashTable, "city", "New York");
    insertKeyValuePair(&hashTable, "email", "alice@example.com");

    // 查找键值对
    const char *key = "city";
    const char *value = searchKeyValuePair(&hashTable, key);

    if (value != NULL) {
        printf("Key: %s, Value: %s\n", key, value);
    } else {
        printf("Key %s not found.\n", key);
    }

    return 0;
}
  1. 键值对和哈希表的定义

    • 定义了键值对的数据结构,包括键和值两个字符串以及指向下一个节点的指针,用于处理冲突。
    • 定义了哈希表的数据结构,包括一个指向键值对链表的数组。
    • 实现了初始化函数,将哈希表的所有元素初始化为NULL。
  2. 哈希函数的实现

    • 哈希函数通过移位和累加的方式将键映射到哈希表的索引位置。
  3. 插入键值对的实现

    • 插入键值对函数将新键值对插入到哈希表中对应索引位置的链表头部。
  4. 查找键值对的实现

    • 查找键值对函数遍历哈希表中对应索引位置的链表,比较每个节点的键与目标键,找到目标键时返回其对应的值。
  5. 测试函数

    • 初始化哈希表,插入一些测试键值对,并调用查找函数查找目标键。
    • 输出查找结果。

总结

通过本文的讲解和代码示例,我们学习了如何用C语言实现哈希表并进行键值对的查找。哈希表是一种高效的查找数据结构,适用于快速查找、插入和删除操作。

 

 

 

 

 

  • 9
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值