中文字幕av专区_日韩电影在线播放_精品国产精品久久一区免费式_av在线免费观看网站

溫馨提示×

如何在Linux中使用Hashtable存儲數據

小樊
85
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
三江| 玉溪市| 元氏县| 四会市| 贺州市| 大安市| 黎川县| 宁武县| 水富县| 涡阳县| 修水县| 华阴市| 桦川县| 邛崃市| 彰化县| 揭西县| 元江| 左云县| 大冶市| 赫章县| 西乌| 南宫市| 盘山县| 余干县| 沈阳市| 潜山县| 高阳县| 麦盖提县| 合山市| 如皋市| 赞皇县| 钟山县| 辰溪县| 客服| 武宣县| 进贤县| 高密市| 怀远县| 密山市| 元谋县| 承德市|