哈希表 C

#include<stdio.h>
#include<stdlib.h>
#define NUM 5
typedef struct List {
    int num;
    char* data;
}List;
List* initList() {
    List* L = (List*)malloc(sizeof(List));
    L->num = 0;
    L->data = (char*)malloc(sizeof(char) * NUM);
    for (int i = 0; i < NUM; i++)
    {
        L->data[i] = 0;
    }
    return L;
}
int hush(int data) {
    return data % NUM;

}
void put(List*L,int data) {
    int index = hush(data);
    int count = 1;
    if (L->data[index]!=0)
    {
        index = hush(hush(data) + count);
        //一直找下去,且用外层的hush保证不越界
        count++;
    }
    L->data[index] = data;
    L->num++;
}
int main()
{
    List* list = initList();
    put(list, 'A');
    put(list, 'F');
    printf("%c\n", list->data[0]);
    printf("%c\n", list->data[1]);
    return 0;
}

 参考:

分类: Data Structure | tyrantlucifer

哈希表是一种常用的数据结构,可以高效地存储和查找数据。在通讯录中使用哈希表可以快速地定位需要查找的联系人信息。 在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
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值