有大佬能帮忙写出来的吗 用c写
1条回答 默认 最新
- yangxr1995 2021-06-23 16:11关注
因为最大月份和每月最大日期固定,所以可用二维数组,数组元素为链表头,hash冲突则将其头插到相同链表
#include <stdio.h> #include <assert.h> #include <stdlib.h> #define MON_MAX 12 #define DAY_MAX 31 struct hash_elemt { struct hash_elemt *list; struct stu *stu; } *buckets[MON_MAX+1][DAY_MAX+1]; struct stu { char *name; }; static struct hash_elemt **hash(int mon, int day) { assert(mon > 0 && mon <= MON_MAX); assert(day > 0 && day <= DAY_MAX); return &buckets[mon][day]; } struct hash_elemt *hash_add(struct stu *stu, int mon, int day) { struct hash_elemt **phead, *new; phead = hash(mon, day); new = malloc(sizeof(struct hash_elemt)); new->stu = stu; new->list = (*phead); (*phead) = new; return new; } void print_stu(struct stu *stu) { printf("stu : %s\n", stu->name); } void print_comm_stu(int mon, int day) { struct hash_elemt **phead,*e; printf("mon(%d), day(%d): \n", mon, day); phead = hash(mon,day); for (e = *phead; e; e= e->list) print_stu(e->stu); } int main() { struct stu s1, s2, s3, s4, s5; s1.name = "aa"; s2.name = "bb"; s3.name = "cc"; s4.name = "dd"; s5.name = "ee"; hash_add(&s1, 1, 1); hash_add(&s2, 1, 1); hash_add(&s3, 1, 1); hash_add(&s4, 2, 1); hash_add(&s5, 2, 1); print_comm_stu(1, 1); print_comm_stu(2, 1); return 0; }
本回答被题主选为最佳回答 , 对您是否有帮助呢?解决 无用评论 打赏 举报
悬赏问题
- ¥15 多址通信方式的抗噪声性能和系统容量对比
- ¥15 winform的chart曲线生成时有凸起
- ¥15 msix packaging tool打包问题
- ¥15 finalshell节点的搭建代码和那个端口代码教程
- ¥15 Centos / PETSc / PETGEM
- ¥15 centos7.9 IPv6端口telnet和端口监控问题
- ¥20 完全没有学习过GAN,看了CSDN的一篇文章,里面有代码但是完全不知道如何操作
- ¥15 使用ue5插件narrative时如何切换关卡也保存叙事任务记录
- ¥20 海浪数据 南海地区海况数据,波浪数据
- ¥20 软件测试决策法疑问求解答