C语言 - 哈希表

#include "uthash.h"

// hash结构体

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

// hash初始化

struct hashtable *my_hash = NULL;

// hash查找

HASH_FIND_INT(my_hash, &my_key, hash2ret);  // 参数二是个指针

// hash添加

HASH_ADD_INT(my_hash, key, hash2add);       // 参数二是key名

// hash删除

HASH_DEL(my_hash, hash2del);
free(hash2del);

// hash清除

HASH_CLEAR(hh, my_hash);                    // 没有free

// hash计数

int count = HASH_COUNT(my_hash);

// hash遍历

struct hashtable *iter;

for (iter = my_hash; iter != NULL; iter = iter->hh.next) {
    printf("key=%d, val=%s \n", iter->key, iter->val);
}

// hash排序

int name_sort(struct my_struct *a, struct my_struct *b) {
    return strcmp(a->name, b->name);
}
HASH_SORT(users, name_sort);

int id_sort(struct my_struct *a, struct my_struct *b) {
    return (a->id - b->id);
}
HASH_SORT(users, id_sort);

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值