UT_hash实现增删改查

key为整型

1.引入库并初始化

代码如下(示例):

#include "uthash.h"
typedef struct {
    int key; //可以就只有key不加value
    int value;
    UT_hash_handle hh;
} Hash;
Hash *hashtable = NULL;

2.增删改查hash数据

2.1 增加和修改 hash_insert(key,val)

/* 插入和修改hash */
void hash_insert(int ikey, int ival) {
    struct Hash* it = find(ikey);
    if (it == NULL) {
        struct Hash* tmp = malloc(sizeof(struct Hash));
        tmp->key = ikey, tmp->val = ival;
        HASH_ADD_INT(hashtable, key, tmp);
    } else {
        it->val = ival;
    }
}

2.2 查找 hash_find(key)

找到返回hash数据,找不到返回NULL

/* 查找hash */
struct hashTable* hash_find(int ikey) {
    struct Hash* tmp;
    HASH_FIND_INT(hashtable, &ikey, tmp);
    return tmp;
}

2.3 删除 hash_delete(key)

传入key值,删除成功返回1,失败返回0

/* 删除hash */
int hash_delete(int ikey) {
	struct Hash* it = find(ikey);
	if (it != NULL) {
		HASH_DEL(hash, it);
	    free(it);
	    it = NULL;
	    return 1;
	}else{
		return 0;
	}

}

总结
最后在把代码整合在一起方便调用

#include "uthash.h"
typedef struct {
    int key; //可以就只有key不加value
    int value;
    UT_hash_handle hh;
} Hash;
Hash *hashtable = NULL;

struct Hash* hash_find(int ikey) {
    struct Hash* tmp;
    HASH_FIND_INT(hashtable, &ikey, tmp);
    return tmp;
}
void hash_insert(int ikey, int ival) {
    struct Hash* it = find(ikey);
    if (it == NULL) {
        struct Hash* tmp = malloc(sizeof(struct Hash));
        tmp->key = ikey, tmp->val = ival;
        HASH_ADD_INT(hashtable, key, tmp);
    } else {
        it->val = ival;
    }
}
int hash_delete(int ikey) {
	struct Hash* it = find(ikey);
	if (it != NULL) {
		HASH_DEL(hashtable, it);
	    free(it);
	    it = NULL;
	    return 1;
	}else{
		return 0;
	}
}

key为字符串string
那么使用的方法依赖于你定义key的方法:指针的方式(char *)还是数组的方式(char a[10])。这个差异是非常重要的。当你的结构体使用的是指针方式,那么你应当使用HASH_ADD_KEYPTR方法;当你使用的数组方式,则需要使用HASH_ADD_STR方法。

#include <string.h>  /* strcpy */
#include <stdlib.h>  /* malloc */
#include <stdio.h>   /* printf */
#include "uthash.h"

struct my_struct {
    char name[10];             /* key (string is WITHIN the structure) */
    int id;
    UT_hash_handle hh;         /* makes this structure hashable */
};


int main(int argc, char *argv[]) {
    const char *names[] = { "joe", "bob", "betty", NULL };
    struct my_struct *s, *tmp, *users = NULL;

    for (int i = 0; names[i]; ++i) {
        s = (struct my_struct *)malloc(sizeof *s);
        strcpy(s->name, names[i]);
        s->id = i;
        HASH_ADD_STR( users, name, s );
    }

    HASH_FIND_STR( users, "betty", s);
    if (s) printf("betty's id is %d\n", s->id);

    /* free the hash table contents */
    HASH_ITER(hh, users, s, tmp) {
      HASH_DEL(users, s);
      free(s);
    }
    return 0;
}

#include <string.h>  /* strcpy */
#include <stdlib.h>  /* malloc */
#include <stdio.h>   /* printf */
#include "uthash.h"

struct my_struct {
    const char *name;          /* key */
    int id;
    UT_hash_handle hh;         /* makes this structure hashable */
};


int main(int argc, char *argv[]) {
    const char *names[] = { "joe", "bob", "betty", NULL };
    struct my_struct *s, *tmp, *users = NULL;

    for (int i = 0; names[i]; ++i) {
        s = (struct my_struct *)malloc(sizeof *s);
        s->name = names[i];
        s->id = i;
        HASH_ADD_KEYPTR( hh, users, s->name, strlen(s->name), s );
    }

    HASH_FIND_STR( users, "betty", s);
    if (s) printf("betty's id is %d\n", s->id);

    /* free the hash table contents */
    HASH_ITER(hh, users, s, tmp) {
      HASH_DEL(users, s);
      free(s);
    }
    return 0;
}

指针key
你可以使用指针(地址)作为key,也就是说指针(地址)本身也可以作为key来使用。也对应HASH_ADD_KEYPTR相关方法。下面是一个实例:

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

typedef struct {
  void *key;
  int i;
  UT_hash_handle hh;
} el_t;

el_t *hash = NULL;
char *someaddr = NULL;

int main() {
  el_t *d;
  el_t *e = (el_t *)malloc(sizeof *e);
  if (!e) return -1;
  e->key = (void*)someaddr;
  e->i = 1;
  HASH_ADD_PTR(hash,key,e);
  HASH_FIND_PTR(hash, &someaddr, d);
  if (d) printf("found\n");

  /* release memory */
  HASH_DEL(hash,e);
  free(e);
  return 0;
}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值