溫馨提示×

如何在Linux中使用Hashtable存儲數(shù)據(jù)

小樊
82
2024-10-01 00:42:12
欄目: 云計算

在 Linux 中,你可以使用 C 語言的標(biāo)準(zhǔn)庫函數(shù) malloc()free() 來動態(tài)地創(chuàng)建和釋放 Hashtable。以下是一個簡單的 Hashtable 實現(xiàn)示例:

  1. 首先,創(chuàng)建一個名為 hashtable.h 的頭文件,用于聲明 Hashtable 結(jié)構(gòu)和相關(guān)函數(shù)原型:
#ifndefhashtable_h
#define hashtable_h

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

#define TABLE_SIZE 100

typedef struct Entry {
    char *key;
    char *value;
    struct Entry *next;
} Entry;

typedef struct {
    Entry *entries[TABLE_SIZE];
} Hashtable;

Hashtable* createTable();
void freeTable(Hashtable *table);
int hash(const char *key);
void insert(Hashtable *table, const char *key, const char *value);
char* get(Hashtable *table, const char *key);
void remove(Hashtable *table, const char *key);
void rehash(Hashtable *table);

#endif //hashtable_h
  1. 接下來,創(chuàng)建一個名為 hashtable.c 的源文件,用于實現(xiàn) Hashtable 結(jié)構(gòu)和相關(guān)函數(shù):
#include "hashtable.h"

Hashtable* createTable() {
    Hashtable *table = (Hashtable *)malloc(sizeof(Hashtable));
    if (table == NULL) {
        fprintf(stderr, "Failed to allocate memory for the table.\n");
        exit(1);
    }

    for (int i = 0; i < TABLE_SIZE; i++) {
        table->entries[i] = NULL;
    }

    return table;
}

void freeTable(Hashtable *table) {
    for (int i = 0; i < TABLE_SIZE; i++) {
        Entry *entry = table->entries[i];
        while (entry != NULL) {
            Entry *temp = entry;
            entry = entry->next;
            free(temp->key);
            free(temp->value);
            free(temp);
        }
    }
    free(table);
}

int hash(const char *key) {
    int hashValue = 0;
    for (int i = 0; key[i] != '\0'; i++) {
        hashValue = (hashValue * 31 + key[i]) % TABLE_SIZE;
    }
    return hashValue;
}

void insert(Hashtable *table, const char *key, const char *value) {
    int index = hash(key);
    Entry *entry = table->entries[index];

    if (entry == NULL) {
        Entry *newEntry = (Entry *)malloc(sizeof(Entry));
        newEntry->key = strdup(key);
        newEntry->value = strdup(value);
        newEntry->next = NULL;
        table->entries[index] = newEntry;
    } else {
        while (entry != NULL) {
            if (strcmp(entry->key, key) == 0) {
                free(entry->value);
                entry->value = strdup(value);
                return;
            }
            entry = entry->next;
        }
        Entry *newEntry = (Entry *)malloc(sizeof(Entry));
        newEntry->key = strdup(key);
        newEntry->value = strdup(value);
        newEntry->next = table->entries[index];
        table->entries[index] = newEntry;
    }
}

char* get(Hashtable *table, const char *key) {
    int index = hash(key);
    Entry *entry = table->entries[index];

    while (entry != NULL) {
        if (strcmp(entry->key, key) == 0) {
            return entry->value;
        }
        entry = entry->next;
    }

    return NULL;
}

void remove(Hashtable *table, const char *key) {
    int index = hash(key);
    Entry *entry = table->entries[index];
    Entry **prev = &table->entries[index];

    while (entry != NULL) {
        if (strcmp(entry->key, key) == 0) {
            *prev = entry->next;
            free(entry->key);
            free(entry->value);
            free(entry);
            return;
        }
        prev = &entry->next;
        entry = entry->next;
    }
}

void rehash(Hashtable *table) {
    Entry **oldEntries = table->entries;
    int oldSize = TABLE_SIZE;

    table = createTable();
    for (int i = 0; i < oldSize; i++) {
        Entry *entry = oldEntries[i];
        while (entry != NULL) {
            insert(table, entry->key, entry->value);
            entry = entry->next;
        }
    }

    free(oldEntries);
}
  1. 編譯并安裝 Hashtable 庫:
gcc -c hashtable.c -o hashtable.o
sudo ld -shared hashtable.o -o libhashtable.so
  1. 在你的應(yīng)用程序中使用 Hashtable 庫:
#include <stdio.h>
#include "hashtable.h"

int main() {
    Hashtable *table = createTable();
    insert(table, "key1", "value1");
    insert(table, "key2", "value2");
    printf("key1: %s\n", get(table, "key1"));
    printf("key2: %s\n", get(table, "key2"));
    remove(table, "key1");
    printf("key1 (after removal): %s\n", get(table, "key1"));
    freeTable(table);
    return 0;
}
  1. 編譯并運行應(yīng)用程序:
gcc -o app app.c -L. -lhhashtable
./app

這個示例展示了如何在 Linux 中使用 Hashtable 存儲數(shù)據(jù)。請注意,這個實現(xiàn)是簡單的,沒有考慮性能優(yōu)化和線程安全。在實際應(yīng)用中,你可能需要使用更高級的哈希表實現(xiàn),例如 libhashuthash。

0