哈希表模拟#define
来源:互联网 发布:淘宝头条怎么发布文章 编辑:程序博客网 时间:2024/05/17 06:48
代码:
#include <stdio.h>#include <string.h>#include <stdlib.h>struct nlist//表的节点{ struct nlist *next; char *name; char *dfen;};#define HASHSIZE 101static struct nlist *hashtab[HASHSIZE] = {NULL};unsigned shash(char *);//算哈希值struct nlist *lookup(struct nlist *);//找节点struct nlist *install(char *, char *);//加节点int main(){}unsigned shash(char *s){ unsigned hashval; for(hashval = 0; *s != '\0'; s++)//计算哈希值,为啥是31。。大概是统计学什么什么的 hashval = *s + 31 * hashval; return hashval % HASHSIZE;}struct nlist *lookup( char * name ){ struct nlist *np; for( np = hashtab[shash(name)]; np != NULL; np = np->next )//通过哈希值查表 if( strcmp(name, p->name) == 0) return p; return NULL;}struct nlist *install(char *name, char *dfen){ struct nlist *np; if( (np = lookup(name)) == NULL)//如果name不存在 { np = (struct nlist*)malloc( sizeof(*np)); if(np == NULL || (np->name = strdup( name )) == NULL)//注意main里最后free return NULL; hashval = shash(name); np->next = hashtab[hashval]; } else//如果存在就删了先前define的内容 //strcpy(np->dfen, dfen); free( (void*)np->defn ); if( (np = strdup(dfen) ) == NULL) return NULL; return np;}
懒得没写main调用。。。
来自《C程序设计语言》
阅读全文
0 0
- 哈希表模拟#define
- #define
- define
- #define
- #define
- #define
- define
- #define
- define
- #define
- #define # ##
- #define
- #define
- define
- define
- #define
- #define
- #define
- phpstorm 免费激活码
- AXJ苹果IOS神器一键新机改串清理超级全息备份支持iOS全系列已越狱设备,超V8,X9!企鹅,WX:78282588
- Ubuntu14.04安装ROS indigo
- 小区广播介绍补充
- 文件加密到图片
- 哈希表模拟#define
- 请求发送者与接收者解耦——命令模式(五)
- 科普:为什么手机摄像头在追求到2000万像素之后反正往回倒退到1200万像素发展了?
- C语言
- 【Anti-Nim】BZOJ1022(SHOI2008)[小约翰的游戏John]题解
- Andriod: The method getDrawable(int) from the type Resources is deprecated,一系列问题
- scipy运用
- 一种排序
- 自顶向下,逐步求精