C語言實現的資料結構之------雜湊表

smstong發表於2016-04-13

1 雜湊表原理

這裡不講高深理論,只說直觀感受。雜湊表的目的就是為了根據資料的部分內容(關鍵字),直接計算出存放完整資料的記憶體地址。

試想一下,如果從連結串列中根據關鍵字查詢一個元素,那麼就需要遍歷才能得到這個元素的記憶體地址,如果連結串列長度很大,查詢就需要更多的時間.

void* list_find_by_key(list,key)
{
    for(p=list;p!=NULL; p=p->next){
        if(p->key == key){
            return p;
        }
        return p;
    }
}

為了解決根據關鍵字快速找到元素的存放地址,雜湊表應運而生。它通過某種演算法(雜湊函式)直接根據關鍵字計算出元素的存放地址,由於無需遍歷,所以效率很高。

void* hash_table_find_by_key(table, key)
{
    void* p = hash(key);
    return p;
}

當然,上面的虛擬碼忽略了一個重要的事實:那就是不同的關鍵字可能產生出同樣的hash值。

hash("張三") = 23;
hash("李四") = 30;
hash("王五") = 23;

這種情況稱為“衝突”,為了解決這個問題,有兩種方法:一是鏈式擴充套件;二是開放定址。這裡只講第一種:鏈式擴充套件。

也就是把具有相同hash值的元素放到一起,形成一個連結串列。這樣在插入和尋找資料的時候就需要進一步判斷。

void* hash_table_find_by_key(table, key)
{
    void* list = hash(key);
    return list_find_by_key(list, key);
}

需要注意的是,只要hash函式合適,這裡的連結串列通常都長度不大,所以查詢效率依然很高。

下圖是一個雜湊表執行時記憶體佈局:

這裡寫圖片描述

2 純C實現原始碼

實際工作中,大多數情況下,關鍵字都是字串的形式,而大多數教科書上卻使用整數關鍵字來舉例,這非常脫離實際。為此,本人決定使用純C語言開發一個雜湊表結構,供大家參考。主要特點:

  • 基於介面開發,對外徹底隱藏實現細節
  • 具有自動釋放客戶結構記憶體的回撥功能
  • 採用經典的Times33雜湊演算法
  • 採用純C開發,可供C和C++客戶使用

HashTable.h 標頭檔案

#pragma once
typedef struct HashTable HashTable;

#ifdef __cplusplus
extern "C" {
#endif

    /* new an instance of HashTable */
    HashTable* hash_table_new();

    /*
    delete an instance of HashTable,
    all values are removed auotmatically.
    */
    void hash_table_delete(HashTable* ht);

    /*
    add or update a value to ht, 
    free_value(if not NULL) is called automatically when the value is removed.
    return 0 if success, -1 if error occurred.
    */
    #define hash_table_put(ht,key,value) hash_table_put2(ht,key,value,NULL);
    int hash_table_put2(HashTable* ht, char* key, void* value, void(*free_value)(void*));

    /* get a value indexed by key, return NULL if not found. */
    void* hash_table_get(HashTable* ht, char* key);

    /* remove a value indexed by key */
    void hash_table_rm(HashTable* ht, char* key);

#ifdef __cplusplus
}
#endif

HashTable.c 實現檔案

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

#define TABLE_SIZE (1024*1024)

/* element of the hash table's chain list */
struct kv
{
    struct kv* next;
    char* key;
    void* value;
    void(*free_value)(void*);
};

/* HashTable */
struct HashTable
{
    struct kv ** table;
};

/* constructor of struct kv */
static void init_kv(struct kv* kv)
{
    kv->next = NULL;
    kv->key = NULL;
    kv->value = NULL;
    kv->free_value = NULL;
}
/* destructor of struct kv */
static void free_kv(struct kv* kv)
{
    if (kv) {
        if (kv->free_value) {
            kv->free_value(kv->value);
        }
        free(kv->key);
        kv->key = NULL;
        free(kv);
    }
}
/* the classic Times33 hash function */
static unsigned int hash_33(char* key)
{
    unsigned int hash = 0;
    while (*key) {
        hash = (hash << 5) + hash + *key++;
    }
    return hash;
}

/* new a HashTable instance */
HashTable* hash_table_new()
{
    HashTable* ht = malloc(sizeof(HashTable));
    if (NULL == ht) {
        hash_table_delete(ht);
        return NULL;
    }
    ht->table = malloc(sizeof(struct kv*) * TABLE_SIZE);
    if (NULL == ht->table) {
        hash_table_delete(ht);
        return NULL;
    }
    memset(ht->table, 0, sizeof(struct kv*) * TABLE_SIZE);

    return ht;
}
/* delete a HashTable instance */
void hash_table_delete(HashTable* ht)
{
    if (ht) {
        if (ht->table) {
            int i = 0;
            for (i = 0; i<TABLE_SIZE; i++) {
                struct kv* p = ht->table[i];
                struct kv* q = NULL;
                while (p) {
                    q = p->next;
                    free_kv(p);
                    p = q;
                }
            }
            free(ht->table);
            ht->table = NULL;
        }
        free(ht);
    }
}

/* insert or update a value indexed by key */
int hash_table_put2(HashTable* ht, char* key, void* value, void(*free_value)(void*))
{
    int i = hash_33(key) % TABLE_SIZE;
    struct kv* p = ht->table[i];
    struct kv* prep = p;

    while (p) { /* if key is already stroed, update its value */
        if (strcmp(p->key, key) == 0) {
            if (p->free_value) {
                p->free_value(p->value);
            }
            p->value = value;
            p->free_value = free_value;
            break;
        }
        prep = p;
        p = p->next;
    }

    if (p == NULL) {/* if key has not been stored, then add it */
        char* kstr = malloc(strlen(key) + 1);
        if (kstr == NULL) {
            return -1;
        }
        struct kv * kv = malloc(sizeof(struct kv));
        if (NULL == kv) {
            free(kstr);
            kstr = NULL;
            return -1;
        }
        init_kv(kv);
        kv->next = NULL;
        strcpy(kstr, key);
        kv->key = kstr;
        kv->value = value;
        kv->free_value = free_value;

        if (prep == NULL) {
            ht->table[i] = kv;
        }
        else {
            prep->next = kv;
        }
    }
    return 0;
}

/* get a value indexed by key */
void* hash_table_get(HashTable* ht, char* key)
{
    int i = hash_33(key) % TABLE_SIZE;
    struct kv* p = ht->table[i];
    while (p) {
        if (strcmp(key, p->key) == 0) {
            return p->value;
        }
        p = p->next;
    }
    return NULL;
}

/* remove a value indexed by key */
void hash_table_rm(HashTable* ht, char* key)
{
    int i = hash_33(key) % TABLE_SIZE;

    struct kv* p = ht->table[i];
    struct kv* prep = p;
    while (p) {
        if (strcmp(key, p->key) == 0) {
            free_kv(p);
            if (p == prep) {
                ht->table[i] = NULL;
            }
            else {
                prep->next = p->next;
            }
        }
        prep = p;
        p = p->next;
    }
}

3 測試程式

下面是測試程式原始碼,基於C++。

測試程式test.cpp

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

#include "HashTable.h"

// 要放入雜湊表中的結構體
struct Student
{
    int age;
    float score;
    char name[32];
    char data[1024 * 1024* 10];
};

// 結構體記憶體釋放函式
static void free_student(void* stu)
{
    free(stu);
}

// 顯示學生資訊的函式
static void show_student(struct Student* p)
{
    printf("姓名:%s, 年齡:%d, 學分:%.2f\n", p->name, p->age, p->score);
}

int main()
{
    // 新建一個HashTable例項
    HashTable* ht = hash_table_new();
    if (NULL == ht) {
        return -1;
    }

    // 向雜湊表中加入多個學生結構體
    for (int i = 0; i < 100; i++) {
        struct Student * stu = (struct Student*)malloc(sizeof(struct Student));
        stu->age = 18 + rand()%5;
        stu->score = 50.0f + rand() % 100;
        sprintf(stu->name, "同學%d", i);
        hash_table_put2(ht, stu->name, stu, free_student);
    }

    // 根據學生姓名查詢學生結構
    for (int i = 0; i < 100; i++) {
        char name[32];
        sprintf(name, "同學%d", i);
        struct Student * stu = (struct Student*)hash_table_get(ht, name);
        show_student(stu);
    }

    // 銷燬雜湊表例項
    hash_table_delete(ht);
    return 0;
}

相關文章