在 Linux 中,你可以使用 C 語言的標準庫函數 malloc()
和 free()
來動態地創建和釋放 Hashtable。以下是一個簡單的 Hashtable 實現示例:
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
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);
}
gcc -c hashtable.c -o hashtable.o
sudo ld -shared hashtable.o -o libhashtable.so
#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;
}
gcc -o app app.c -L. -lhhashtable
./app
這個示例展示了如何在 Linux 中使用 Hashtable 存儲數據。請注意,這個實現是簡單的,沒有考慮性能優化和線程安全。在實際應用中,你可能需要使用更高級的哈希表實現,例如 libhash 或 uthash。