两数相加使用哈希表C语言详解

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;
}
/*在上述代码中,第一个参数hashtable是哈希表,第二个参数是ikey的地址(一定要传递地址)。
最后tem是输出变量。当可以在哈希表中找到相应键值时,temp返回给定键的结构,当找不到时temp
返回NULL*/

void insert(int ikey,int ival){
/*重复性检查,当把两个相同key值的结构体添加到哈希表中时会报错*/
    struct hashTable* it = find(ikey);
 /*只有在哈希中不存在ID的情况下,我们才创建该项目并将其添加。
否则,我们只修改已经存在的结构。*/
    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;
    }
}

/*
HASH_ADD_INT表示添加的键值为int类型 HASH_ADD_STR表示添加的键值为字符串类型 
HASH_ADD_PTR表示添加的键值为指针类型 HASH_ADD表示添加的键值可以是任意类型
HASH_ADD_INT函数中,第一个参数hashtable是哈希表,第二个参数ikey是键字段
  • 0
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
哈希表是一种常用的数据结构,可以高效地存储和查找数据。在通讯录中使用哈希表可以快速地定位需要查找的联系人信息。 在C语言中,哈希表的设计主要分为两个部分:哈希函数和哈希表结构。 哈希函数是将关键字映射到哈希表中的位置的函数。常用的哈希函数有取余法、乘法散列法、平方取中法等。在通讯录中,我们可以使用联系人姓名或者电话号码作为关键字,通过哈希函数将其映射到哈希表中。 哈希表结构包含了哈希表的基本元素,包括哈希表数组、存储数据的结构体以及相关操作函数。在通讯录中,我们可以使用一个结构体来存储联系人的信息,如姓名、电话号码等,然后将其存储到哈希表中。同时,我们需要设计插入、删除、查找等操作函数来对哈希表进行操作。 以下是一个简单的哈希表通讯录的C语言代码示例: ```c #include <stdio.h> #include <stdlib.h> #include <string.h> #define MAX_LEN 100 // 姓名最大长度 #define HASH_SIZE 101 // 哈希表大小 // 联系人结构体 typedef struct Contact { char name[MAX_LEN]; char phone[MAX_LEN]; struct Contact *next; } Contact; // 哈希表结构体 typedef struct HashTable { Contact *table[HASH_SIZE]; } HashTable; // 哈希函数 int hash(char *name) { int len = strlen(name); int sum = 0; for (int i = 0; i < len; i++) { sum += name[i]; } return sum % HASH_SIZE; } // 初始化哈希表 void init(HashTable *ht) { for (int i = 0; i < HASH_SIZE; i++) { ht->table[i] = NULL; } } // 插入联系人 void insert(HashTable *ht, char *name, char *phone) { int index = hash(name); Contact *node = (Contact *)malloc(sizeof(Contact)); strcpy(node->name, name); strcpy(node->phone, phone); node->next = NULL; if (ht->table[index] == NULL) { ht->table[index] = node; } else { Contact *temp = ht->table[index]; while (temp->next != NULL) { temp = temp->next; } temp->next = node; } } // 查找联系人 Contact *find(HashTable *ht, char *name) { int index = hash(name); Contact *temp = ht->table[index]; while (temp != NULL) { if (strcmp(temp->name, name) == 0) { return temp; } temp = temp->next; } return NULL; } // 删除联系人 void delete(HashTable *ht, char *name) { int index = hash(name); Contact *temp = ht->table[index]; if (temp == NULL) { return; } else if (strcmp(temp->name, name) == 0) { ht->table[index] = temp->next; free(temp); return; } while (temp->next != NULL && strcmp(temp->next->name, name) != 0) { temp = temp->next; } if (temp->next != NULL) { Contact *del = temp->next; temp->next = del->next; free(del); return; } } int main() { HashTable ht; init(&ht); // 插入联系人 insert(&ht, "张三", "123456"); insert(&ht, "李四", "234567"); // 查找联系人 Contact *contact1 = find(&ht, "张三"); if (contact1 != NULL) { printf("姓名:%s,电话:%s\n", contact1->name, contact1->phone); } // 删除联系人 delete(&ht, "张三"); return 0; } ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

永远新手的小猿

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

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

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

打赏作者

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

抵扣说明:

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

余额充值