亚洲激情专区-91九色丨porny丨老师-久久久久久久女国产乱让韩-国产精品午夜小视频观看

溫馨提示×

如何在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
南澳县| 疏附县| 奉节县| 拜泉县| 左云县| 勃利县| 安宁市| 巴青县| 台江县| 松滋市| 大余县| 福清市| 泽州县| 连州市| 闸北区| 丹巴县| 花莲市| 洛宁县| 轮台县| 河西区| 怀宁县| 随州市| 阿拉善右旗| 永仁县| 汾西县| 金湖县| 锦州市| 麻阳| 武威市| 珲春市| 剑阁县| 英山县| 龙州县| 同江市| 阿拉善盟| 景德镇市| 彭山县| 金华市| 谷城县| 土默特左旗| 莱阳市|