c实现hash函数双链表

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

#define DEFAULT_SIZE   16

/* * 
 * double link 
 * */
typedef struct list_node_s
{
    int                 key;
    void               *data;                     
 
    struct list_node_s *prev;
    struct list_node_s *next;
} list_node_t;

typedef struct hash_table_s
{
    list_node_t **hash_map;
    int           table_size;
} hash_talbe_t;

int hash_func(int key, int table_size) {
    return (key % table_size);
}

hash_talbe_t *init_hash(int table_size) {
    if (table_size <= 0) {
        return NULL;
    }

    hash_talbe_t *hash_map = (hash_talbe_t *)calloc(1, sizeof(hash_talbe_t));
    if (hash_map == NULL) {
        return NULL;
    }

    hash_map->table_size = table_size;
    hash_map->hash_map = (list_node_t **)calloc(1, sizeof(list_node_t *) * table_size);
    if (hash_map->hash_map == NULL) {
        free(hash_map);
        return NULL;
    }

    return hash_map;
}

list_node_t *find(hash_talbe_t *hash_map, int key) {
    if (!hash_map) {
        return NULL;
    }
    
    int index = hash_func(key, hash_map->table_size);
    if (hash_map->hash_map[index] == NULL) {
        return NULL;

    } else {
        list_node_t *head = hash_map->hash_map[index];
        if (head->key == key) {
            return head;
        }

        list_node_t *tmp = head->next;
        while (tmp != head) {
            if (key == tmp->key) {
                return tmp;
            }

            tmp = tmp->next;
        }
    }

    return NULL;
}

void insert(hash_talbe_t *hash_map, int key, void *value) {
    if (!hash_map || !value) {
        return;
    }

    list_node_t *node = find(hash_map, key);
    if (node) {
        node->data = value;

        return;
    }

    node = (list_node_t *)calloc(1, sizeof(list_node_t));
    if (node == NULL) {
        return;
    }

    node->data = value;
    node->key = key;
    node->prev = node->next = node;

    int index = hash_func(key, hash_map->table_size);
    if (hash_map->hash_map[index] == NULL) {
        hash_map->hash_map[index] = node;

    } else {
        list_node_t *head = hash_map->hash_map[index];

        node->prev = head->prev;
        node->next = head->prev->next;
        head->prev->next = node;
        head->prev = node;
    }
}

void delete(hash_talbe_t *hash_map, int key) {
    if (!hash_map) {
        return;
    }

    list_node_t *node = find(hash_map, key);
    if (node == NULL) {
        return;
    }

    node->prev->next = node->next;
    node->next->prev = node->prev;

    free(node);
}

int main(void) {
    char *elements[] = {
        (char *)"xiaomi",
        (char *)"huawei",
        (char *)"dajiang"
    };

    hash_talbe_t *hash_map = init_hash(DEFAULT_SIZE);

    insert(hash_map, 1, elements[0]);
    insert(hash_map, 2, elements[1]);
    insert(hash_map, 3, elements[2]);

    delete(hash_map, 1);

    for (int i = 0; i < DEFAULT_SIZE; i++) {
        list_node_t *node = find(hash_map, i);
        if (node) {
            printf("key:%d, value:%s\n", node->key, (char *)node->data);
        }
    }

    return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

前进的蜗牛啊

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

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

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

打赏作者

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

抵扣说明:

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

余额充值