[译]C语言实现一个简易的Hash table(2)

上一章,简单介绍了Hash Table,并提出了本教程中要实现的几个Hash Table的方法,有search(a, k)insert(a, k, v)delete(a, k),本章将介绍Hash table使用的数据结构。数组

Hash table数据结构

hash表中存储的每一项key-value的数据结构:数据结构

// hash_table.h
typedef struct {
    char* key;
    char* value;
} ht_item;

咱们的hash表中保存着一个指向每一项的指针数组,里面还包括hash表的大小,结构以下:函数

// hash_table.h
typedef struct {
    int size;
    int count;
    ht_item** items;
} ht_hash_table;

初始化与删除

hash表中,咱们须要定义一个函数来初始化一条记录(ht_item),这个函数会为每一条记录(ht_item)申请内存,而后将kv保存在这个内存中。为了让该函数只能在咱们的hash table中使用,咱们用static来修饰。.net

// hash_table.c
#include <stdlib.h>
#include <string.h>

#include "hash_table.h"

static ht_item* ht_new_item(const char* k, const char* v) {
    ht_item* i = malloc(sizeof(ht_item));
    i->key = strdup(k);  // 复制操做
    i->value = strdup(v);
    return i;
}

ht_new初始化一个新的hash表size表示这个hash表能够存储多少条记录,如今是固定的53条。咱们将在后面讲解如何扩充这个hash表,咱们使用calloc函数来初始化一条记录(若是对calloc不熟悉,能够看看我这篇文章:https://my.oschina.net/simonWang/blog/2998978),calloc会申请一片空间并用NULL来填充,记录为NULL就表明空的。指针

// hash_table.c
ht_hash_table* ht_new() {
    ht_hash_table* ht = malloc(sizeof(ht_hash_table));

    ht->size = 53;
    ht->count = 0;
    ht->items = calloc((size_t)ht->size, sizeof(ht_item*));
    return ht;
}

咱们还须要额外的函数来删除ht_itemht_hash_table,这个函数会释放(free)咱们以前申请的内存空间,以致于不会形成内存泄漏:code

// hash_table.c
static void ht_del_item(ht_item* i) {
    free(i->key);
    free(I->value);
    free(i);
}

void ht_delete_hash_table(ht_hash_table* ht) {
    for (int i = 0; i < ht->size; ++i) {
        ht_item* item = ht_items[I];
        if (item != NULL) {
            ht_del_item(item);
        }
    }
    free(ht->items);
    free(ht);
}

如今,咱们已经完成定义一个hash表,如今咱们能够试着建立一个hash表并试着销毁它,尽管如今并无作太多东西。blog

// main.c
#include hash_table.h

int main() {
    ht_hash_table* ht = ht_new();
    ht_del_hash_table(ht);
}

上一章:hash表介绍 下一章:hash函数教程

相关文章
相关标签/搜索