c语言实现HashTable

概念:哈希表是一种数据结构,它通过将键映射到数组的某个位置来存储和检索值。

第一步,首先定义节点

typedef struct Node {
    char *key;
    int value;
    struct Node *next;
} Node;

这里,我定义的键是字符,value是整数。

第二步,自定义hash算法

int hash(char *key) {
    int sum = 0;
    for (int i = 0; i < strlen(key); i++) {
        sum += key[i];
    }
    return sum;
}

这里的哈希函数是一个简单的求和函数,返回的是键中的每个字符的ASCII值相加的和

第三步,创建节点

Node *createNode(char *key, int value) {
    Node *newNode = (Node *)malloc(sizeof(Node));
    newNode->key = strdup(key);
    newNode->value = value;
    newNode->next = NULL;
    return newNode;
}

strdup() 函数将参数 key 指向的字符串复制到一个字符串指针上去,这个字符串指针事先可以没被初始化。在复制时,strdup() 会给这个指针分配空间,使用 malloc() 函数进行分配,如果不再使用这个指针,相应的用 free() 来释放掉这部分空间。

第四步,插入节点

void insert(Node **table, char *key, int value) {
    int index = hash(key) % TABLE_SIZE;
    Node *newNode = createNode(key, value);
    if (table[index] == NULL) {
        table[index] = newNode;
    } else {
        Node *current = table[index];
        while (current->next != NULL) {
            current = current->next;
        }
        current->next = newNode;
    }
}

之前的hash算法%TABLE_SIZE,得出索引,如果索引所在的位置上为null,就直接存放,否则就在当前索引的位置的next上看是否为null,这里用了while循环

第五步,搜索节点

int search(Node **table, char *key) {
    int index = hash(key) % TABLE_SIZE;
    Node *current = table[index];
    while (current != NULL) {
        if (strcmp(current->key, key) == 0) {
            return current->value;
        }
        current = current->next;
    }
    return -1;
}

搜索节点,同样使用hash函数得到索引,再用 strcmp()函数来判断,如果为0,那就是那个值。否则就继续while循环找当前索引的下一个,因为存进去的时候就是这样存的。

第六步,删除节点

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

简单遍历,然后free掉。

完整代码:

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

#define TABLE_SIZE 10

typedef struct Node {
    char *key;
    int value;
    struct Node *next;
} Node;
int hash(char *key) {
    int sum = 0;
    for (int i = 0; i < strlen(key); i++) {
        sum += key[i];
    }
    return sum;
}
Node *createNode(char *key, int value) {
    Node *newNode = (Node *)malloc(sizeof(Node));
    newNode->key = strdup(key);
    newNode->value = value;
    newNode->next = NULL;
    return newNode;
}

void insert(Node **table, char *key, int value) {
    int index = hash(key) % TABLE_SIZE;
    Node *newNode = createNode(key, value);
    if (table[index] == NULL) {
        table[index] = newNode;
    } else {
        Node *current = table[index];
        while (current->next != NULL) {
            current = current->next;
        }
        current->next = newNode;
    }
}

int search(Node **table, char *key) {
    int index = hash(key) % TABLE_SIZE;
    Node *current = table[index];
    while (current != NULL) {
        if (strcmp(current->key, key) == 0) {
            return current->value;
        }
        current = current->next;
    }
    return -1;
}



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

int main() {
    Node **table = (Node **)malloc(TABLE_SIZE * sizeof(Node *));
    for (int i = 0; i < TABLE_SIZE; i++) {
        table[i] = NULL;
    }

    insert(table, "apple", 1);
    insert(table, "banana", 2);
    insert(table, "orange", 3);

    printf("apple: %d\n", search(table, "apple"));
    printf("banana: %d\n", search(table, "banana"));
    printf("orange: %d\n", search(table, "orange"));
    printf("grape: %d\n", search(table, "grape"));

    freeTable(table);
    return 0;
}

运行结果:

  • 11
    点赞
  • 13
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
以下是一个简单的哈希表C语言实现代码,使用链地址法解决哈希冲突: ```c #include <stdio.h> #include <stdlib.h> #include <string.h> #define HASHSIZE 12 #define NULLKEY -32768 typedef struct { int *elem; // 数据元素存储基址,动态分配数组 int count; // 当前数据元素个数 } HashTable; int InitHashTable(HashTable *H) { int i; H->count = HASHSIZE; H->elem = (int *)malloc(HASHSIZE * sizeof(int)); for (i = 0; i < HASHSIZE; i++) { H->elem[i] = NULLKEY; } return 1; } int Hash(int key) { return key % HASHSIZE; // 散列函数采用除留余数法 } void InsertHash(HashTable *H, int key) { int addr = Hash(key); // 求哈希地址 while (H->elem[addr] != NULLKEY) { // 如果不为空,则冲突 addr = (addr + 1) % HASHSIZE; // 开放定址法的线性探测 } H->elem[addr] = key; // 直到有空位后插入关键字 } int SearchHash(HashTable H, int key) { int addr = Hash(key); // 求哈希地址 while (H.elem[addr] != key) { // 如果不相等,则冲突 addr = (addr + 1) % HASHSIZE; // 开放定址法的线性探测 if (H.elem[addr] == NULLKEY || addr == Hash(key)) { // 如果循环回到原点或者遇到空位,则说明关键字不存在 return -1; } } return addr; // 找到关键字,返回其地址 } int main() { HashTable H; int i, key, result; InitHashTable(&H); printf("请输入%d个数:\n", HASHSIZE); for (i = 0; i < HASHSIZE; i++) { scanf("%d", &key); InsertHash(&H, key); } printf("哈希表中的数据为:\n"); for (i = 0; i < HASHSIZE; i++) { printf("%d ", H.elem[i]); } printf("\n请输入要查找的数:\n"); scanf("%d", &key); result = SearchHash(H, key); if (result == -1) { printf("没有找到%d\n", key); } else { printf("%d的位置是%d\n", key, result); } return 0; } ```

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

nanshaws

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值