linux

如何在Linux中使用Hashtable存储数据

小樊
88
2024-10-01 00:42:12
栏目: 云计算

在 Linux 中,你可以使用 C 语言的标准库函数 malloc()free() 来动态地创建和释放 Hashtable。以下是一个简单的 Hashtable 实现示例:

  1. 首先,创建一个名为 hashtable.h 的头文件,用于声明 Hashtable 结构和相关函数原型:
#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. 接下来,创建一个名为 hashtable.c 的源文件,用于实现 Hashtable 结构和相关函数:
#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. 在你的应用程序中使用 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. 编译并运行应用程序:
gcc -o app app.c -L. -lhhashtable
./app

这个示例展示了如何在 Linux 中使用 Hashtable 存储数据。请注意,这个实现是简单的,没有考虑性能优化和线程安全。在实际应用中,你可能需要使用更高级的哈希表实现,例如 libhashuthash

0
看了该问题的人还看了