哈希表的实现
来源:互联网 发布:二手书店网站知乎 编辑:程序博客网 时间:2024/05/23 19:15
hash.h
#ifndef HASH_H#define HASH_H1#include <stdio.h>#include <time.h>#include <string.h>#include <malloc.h>#include <list.h>#define NMN_USER_HASH_LIST_MAX1024struct user {struct hlist_node sibling;charusername[32];/* *charuserpwd[NMN_VALUE_MAX_LEN]; */time_tstarttime;time_tendtime;};extern int user_count;extern int hash_test[NMN_USER_HASH_LIST_MAX];extern struct hlist_head hash_listhead[NMN_USER_HASH_LIST_MAX];/*int str2user(struct user *user_entry, const char *username, const char *userpwd);int user2str(char *buf, int buf_len, const struct user *user_entry);//*/int read_users_list(const char *file_path);struct user *user_hash_get(const char *username);//#undef HASH_LIST_MAX#endif /* HASH_H */
hash.c
#include <hash.h>struct hlist_head hash_listhead[NMN_USER_HASH_LIST_MAX];int user_count;static unsigned int bkdrhash(const char *str){unsigned int seed = 1313; //13 131 1313 13131 131313 etc...unsigned int hash = 0;while(*str) {hash = hash * seed + (unsigned int)(*str++);}return hash & 0x7FFFFFFF;}static int get_name_hash_index(const char *username){unsigned int key = bkdrhash(username);int index = key & (NMN_USER_HASH_LIST_MAX-1);return index;}static int user_hash_push(struct user *entry){int index = get_name_hash_index(entry->username);hlist_add_head(&entry->sibling, &hash_listhead[index]);return 0;}/*static int user_cmp(const struct user *a, const struct user *b){return strcmp(a->username, b->username);}//*/struct user *user_hash_get(const char *username){struct user *tpos;struct user *ret = NULL;struct hlist_node *pos;int index = get_name_hash_index(username);hlist_for_each_entry(tpos, pos, &hash_listhead[index], sibling) {fprintf(stdout, "username=%s...\n", tpos->username);if(0==strcmp(username, tpos->username)) {ret = tpos;break;}}return ret;}/*static int line2user(struct user *entry, const char *line){sscanf(line, "%s Cleartext-Password := \"%[^\"]%*[ #\"]%ld-%ld", entry->username, entry->userpwd, &entry->starttime, &entry->endtime);return 0;}//*//* without userpwd */static int line2user(struct user *entry, const char *line){return sscanf(line, "%s Cleartext-Password := \"%*[^\"]%*[ #\"]%ld-%ld", entry->username, &entry->starttime, &entry->endtime);}int read_users_list(const char *dir_path){int ret = 0;user_count = 0;char file_path[1024];snprintf(file_path, sizeof(file_path), "%s", dir_path);FILE *fp = fopen(file_path, "r");if(NULL==fp) {return -1;}char line[1024];struct user *entry;while(NULL != (fgets(line, sizeof(line), fp))) {if(('#'==*line) || (!strstr(line, "Cleartext-Password"))) {continue;}entry = (struct user *)malloc(sizeof(struct user));if(!entry) {ret = -1;break;}line2user(entry, line);user_hash_push(entry);++user_count;}fclose(fp);return ret;}/* 暂无释放链表方法实现 */
- 哈希表的实现
- 哈希表的实现
- 哈希表的C实现
- 一个哈希表的实现
- 哈希表的实现
- 哈希表的C实现
- 哈希表的实现
- VB哈希表的实现
- 哈希表的实现
- c++ 哈希表的实现
- 简单的哈希表实现
- 哈希表的简单实现
- PHP的哈希表实现
- 哈希表的C++实现
- 哈希表的C++实现
- hash哈希表的实现
- 哈希表的实现
- 哈希表的C++实现
- 巨人网络3D页游《冒牌天神》
- 哲理故事与管理之道(13)-满足下属不同的需求
- Android C2DM学习——云端推送
- 前10名的书籍
- Android ListView拖动时背景颜色会变成黑色 的解决办法
- 哈希表的实现
- inittab详解
- Android开发中的单元测试-初级教程(01)
- On GUI Architecture: GUI应用的若干问题和模式
- UIApplication keyWindow rootViewController 为 nil 的问题
- EQ管理的四步曲
- C#页面清除缓存
- Servlet
- Java和指针