【C8】一些常用的库



2023年02月11日    Author:Guofei

文章归类: C    文章编号: 10008

版权声明:本文作者是郭飞。转载随意,但需要标明原文链接,并通知本人
原文链接:https://www.guofei.site/2023/02/11/c8.html


uthash

一个实现 Hash 的库

使用方法:

  • 把 src/uthash.h 复制到本地
  • #include "uthash.h"

基本操作

  • 增 add
  • 删 delete
  • 改 replace
  • 查 find
  • 迭代器 iterate
  • 排序 sort

使用

我写的:(排序就先不写了)

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

struct hashTable {
    int key;
    int val;
    UT_hash_handle hh;
};

struct hashTable *hashtable;

//查
struct hashTable *find(int ikey) {
    struct hashTable *tmp;
    HASH_FIND_INT(hashtable, &ikey, tmp);
    return tmp;
}

//插入、替换
void insert(int ikey, int ival) {
    struct hashTable *it = find(ikey);
    if (it == NULL) {
        struct hashTable *tmp = malloc(sizeof(struct hashTable));
        tmp->key = ikey, tmp->val = ival;
        HASH_ADD_INT(hashtable, key, tmp);
    } else {
        it->val = ival;
    }
}

//删除
void delete(int ikey) {
    struct hashTable *it = find(ikey);
    if (it != NULL) {
        HASH_DEL(hashtable, it);
        free(it); // ???有必要吗?
    }
};

//清空
void delete_all() {
    struct hashTable *current, *tmp;
    HASH_ITER(hh, hashtable, current, tmp) {
        HASH_DEL(hashtable, current);
        free(current);
    }
};

void print_hash() {
    struct hashTable *it;
    for (it = hashtable; it != NULL; it = (struct hashTable *) (it->hh.next)) {
        printf("key = %d, val = %d\n", it->key, it->val);
    }
};


int main() {
    hashtable = NULL; // 千万不能少,不然多次调用会有问题
    insert(1, 2);
    insert(2, 8);
    insert(3, 9);
    print_hash();

    printf("==========\n");
    insert(3, 3);
    print_hash();

    delete(2);
    printf("==========\n");

    print_hash();

    delete_all();
    printf("==========\n");

    print_hash();

}

也可以很简单的把key和val改成 char[](字符串)

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

struct hashTable {
    char key[10];
    char val[10];
    UT_hash_handle hh;
};

struct hashTable *hashtable;

//查
struct hashTable *find(char *ikey) {
    struct hashTable *tmp;
    HASH_FIND_STR(hashtable, ikey, tmp);
    return tmp;
}

//插入、替换
void insert(char *ikey, char *ival) {
    struct hashTable *it = find(ikey);
    if (it == NULL) {
        struct hashTable *tmp = malloc(sizeof(struct hashTable));
        strcpy(tmp->key, ikey);
        strcpy(tmp->val, ival);
        HASH_ADD_STR(hashtable, key, tmp);
    } else {
        strcpy(it->val, ival);
    }
}

//删除
void delete(char *ikey) {
    struct hashTable *it = find(ikey);
    if (it != NULL) {
        HASH_DEL(hashtable, it);
        free(it); // ???有必要吗?
    }
};

//清空
void delete_all() {
    struct hashTable *current, *tmp;
    HASH_ITER(hh, hashtable, current, tmp) {
        HASH_DEL(hashtable, current);
        free(current);
    }
};

void print_hash() {
    struct hashTable *it;
    for (it = hashtable; it != NULL; it = (struct hashTable *) (it->hh.next)) {
        printf("key = %s, val = %s\n", it->key, it->val);
    }
};


int main() {
    hashtable = NULL; // 千万不能少,不然多次调用会有问题
    insert("a", "2");
    insert("b", "8");
    insert("c", "9");
    print_hash();

    printf("==========\n");
    insert("c", "3");
    print_hash();

    delete("b");
    printf("==========\n");

    print_hash();

    struct hashTable *res = find("a");
    if (res != NULL) {
        printf("%s,%s", res->key, res->val);
    }


    delete_all();
    printf("==========\n");
    print_hash();
}

参考:https://blog.csdn.net/a123441/article/details/89045293


您的支持将鼓励我继续创作!