以下是一个基于链表实现的哈希表的C语言代码:

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

#define TABLE_SIZE 100

// 链表结构体
typedef struct node {
    char* key;
    int value;
    struct node* next;
} node;

// 哈希表结构体
typedef struct hash_table {
    node** nodes;
} hash_table;

// 创建节点
node* create_node(char* key, int value) {
    node* new_node = (node*)malloc(sizeof(node));
    new_node->key = strdup(key);
    new_node->value = value;
    new_node->next = NULL;
    return new_node;
}

// 销毁节点
void destroy_node(node* n) {
    free(n->key);
    free(n);
}

// 创建哈希表
hash_table* create_hash_table() {
    hash_table* new_table = (hash_table*)malloc(sizeof(hash_table));
    new_table->nodes = (node**)malloc(sizeof(node*) * TABLE_SIZE);
    int i;
    for (i = 0; i < TABLE_SIZE; i++) {
        new_table->nodes[i] = NULL;
    }
    return new_table;
}

// 计算哈希值
unsigned int hash(char* key) {
    unsigned int hashval = 0;
    for (; *key != '\0'; key++) {
        hashval = *key + (hashval << 5) - hashval;
    }
    return hashval % TABLE_SIZE;
}

// 插入键值对
void hash_table_insert(hash_table* ht, char* key, int value) {
    unsigned int hashval = hash(key);
    node* head = ht->nodes[hashval];
    node* current = head;
    while (current != NULL) {
        if (strcmp(current->key, key) == 0) {
            current->value = value;
            return;
        }
        current = current->next;
    }
    node* new_node = create_node(key, value);
    new_node->next = head;
    ht->nodes[hashval] = new_node;
}

// 查找键对应的值
int hash_table_lookup(hash_table* ht, char* key) {
    unsigned int hashval = hash(key);
    node* current = ht->nodes[hashval];
    while (current != NULL) {
        if (strcmp(current->key, key) == 0) {
            return current->value;
        }
        current = current->next;
    }
    return -1;
}

// 删除键值对
void hash_table_delete(hash_table* ht, char* key) {
    unsigned int hashval = hash(key);
    node* head = ht->nodes[hashval];
    node* current = head;
    node* previous = NULL;
    while (current != NULL) {
        if (strcmp(current->key, key) == 0) {
            if (previous == NULL) {
                head = current->next;
            } else {
                previous->next = current->next;
            }
            destroy_node(current);
            ht->nodes[hashval] = head;
            return;
        }
        previous = current;
        current = current->next;
    }
}

// 销毁哈希表
void destroy_hash_table(hash_table* ht) {
    int i;
    for (i = 0; i < TABLE_SIZE; i++) {
        node* current = ht->nodes[i];
        while (current != NULL) {
            node* tmp = current;
            current = current->next;
            destroy_node(tmp);
        }
    }
    free(ht->nodes);
    free(ht);
}

// 测试哈希表
int main() {
    hash_table* ht = create_hash_table();
    hash_table_insert(ht, "apple", 1);
    hash_table_insert(ht, "banana", 2);
    hash_table_insert(ht, "orange", 3);
    printf("%d\n", hash_table_lookup(ht, "apple"));
    printf("%d\n", hash_table_lookup(ht, "banana"));
    printf("%d\n", hash_table_lookup(ht, "orange"));
    hash_table_delete(ht, "banana");
    printf("%d\n", hash_table_lookup(ht, "banana"));
    destroy_hash_table(ht);
    return 0;
}
``
用c语言写个哈希表

原文地址: https://www.cveoy.top/t/topic/eD3x 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录