hashtable 实现
#include <stdlib.h>
#include <stdio.h>
#include <string.h> typedef struct _hashnode{
int val;
char * key;
struct _hashnode * next;
} hashnode; //存储hash表大小
#define HASHTABLE_MAX 1000
//form php source
static inline unsigned int hashtable_hash_str(const char *arKey)
{
register unsigned int hash = ;
unsigned int nKeyLength=strlen(arKey);
/* variant with the hash unrolled eight times */
for (; nKeyLength >= ; nKeyLength -= ) {
hash = ((hash << ) + hash) + *arKey++;
hash = ((hash << ) + hash) + *arKey++;
hash = ((hash << ) + hash) + *arKey++;
hash = ((hash << ) + hash) + *arKey++;
hash = ((hash << ) + hash) + *arKey++;
hash = ((hash << ) + hash) + *arKey++;
hash = ((hash << ) + hash) + *arKey++;
hash = ((hash << ) + hash) + *arKey++;
}
switch (nKeyLength) {
case : hash = ((hash << ) + hash) + *arKey++; /* fallthrough... */
case : hash = ((hash << ) + hash) + *arKey++; /* fallthrough... */
case : hash = ((hash << ) + hash) + *arKey++; /* fallthrough... */
case : hash = ((hash << ) + hash) + *arKey++; /* fallthrough... */
case : hash = ((hash << ) + hash) + *arKey++; /* fallthrough... */
case : hash = ((hash << ) + hash) + *arKey++; /* fallthrough... */
case : hash = ((hash << ) + hash) + *arKey++; break;
case : break;
}
return hash;
} hashnode * hashtable_create(){
hashnode * hashs=(hashnode *)malloc(HASHTABLE_MAX * sizeof(hashnode *));
memset(hashs, , HASHTABLE_MAX * sizeof(hashnode *));
return hashs;
} void hashtable_set(hashnode* hashlist[],const char * b,int val){
unsigned int pos=hashtable_hash_str(b)%HASHTABLE_MAX;
hashnode * hashp= hashlist[pos];
while(hashp){
if(strcmp(hashp->key,b)==){
break;
}
hashp=hashp->next;
}
if(!hashp){
hashnode * p=(hashnode *)malloc(sizeof(hashnode));
memset(p,,sizeof(hashnode));
char * ikey=(char *)malloc(sizeof(char)*(strlen(b)+));
strcpy(ikey,b);
p->key=ikey;
p->val=val;
p->next=hashlist[pos];
hashlist[pos]=p;
}else{
hashp->val=val;
}
} int hashtable_get(hashnode* hashlist[],const char * b,int *r){
unsigned int pos=hashtable_hash_str(b)%HASHTABLE_MAX;
hashnode * hashp= hashlist[pos];
while(hashp){
if(strcmp(hashp->key,b)==){
*r=hashp->val;
return ;
}
hashp=hashp->next;
}
return ;
} void hashtable_unset(hashnode* hashlist[],const char * b){
unsigned int pos=hashtable_hash_str(b)%HASHTABLE_MAX;
hashnode * hashp= hashlist[pos];
hashnode * phashp=NULL;
while(hashp){
if(strcmp(hashp->key,b)==){
if(phashp)
phashp->next=hashp->next;
else
hashlist[pos]=hashp->next;
free(hashp->key);
free(hashp); }
phashp=hashp;
hashp=hashp->next;
}
}
void hashtable_free(hashnode* hashlist[]){
int i=;
while(i<HASHTABLE_MAX){
hashnode * temp=hashlist[i];
while(temp){
hashnode * tt=temp->next;
free(temp->key);
free(temp);
temp=tt;
}
i++;
}
hashlist=NULL;
} int main(){
//使用
hashnode * ht=hashtable_create();
hashtable_set(ht,"abc",);
int r=;
if(hashtable_get(ht,"abc",&r))
printf("%d",r);
hashtable_unset(ht,"abc");
if(hashtable_get(ht,"abc",&r))
printf("%d",r);
//释放所占内存
hashtable_free(ht);
return EXIT_SUCCESS;
}
hashtable 实现的更多相关文章
- HashSet HashTable 与 TreeSet
HashSet<T>类 HashSet<T>类主要是设计用来做高性能集运算的,例如对两个集合求交集.并集.差集等.集合中包含一组不重复出现且无特性顺序的元素. HashSet& ...
- Javascript实现HashTable类
散列算法可以尽快在数据结构中找出指定的一个值,因为可以通过Hash算法求出值的所在位置,存储和插入的时候都按照Hash算法放到指定位置. <script> function HashTab ...
- Java集合专题总结(1):HashMap 和 HashTable 源码学习和面试总结
2017年的秋招彻底结束了,感觉Java上面的最常见的集合相关的问题就是hash--系列和一些常用并发集合和队列,堆等结合算法一起考察,不完全统计,本人经历:先后百度.唯品会.58同城.新浪微博.趣分 ...
- java面试题——HashMap和Hashtable 的区别
一.HashMap 和Hashtable 的区别 我们先看2个类的定义 public class Hashtable extends Dictionary implements Map, Clonea ...
- Map集合及与Collection的区别、HashMap和HashTable的区别、Collections、
特点:将键映射到值的对象,一个映射不能包含重复的键,每个键最多只能映射到一个值. Map集合和Collection集合的区别 Map集合:成对出现 (情侣) ...
- HashTable初次体验
用惯了数组.ArryList,初次接触到HashTable.Dictionary这种字典储存对于我来说简直就是高大上. 1.到底什么是HashTable HashTable就是哈希表,和数组一样,是一 ...
- HashMap和 Hashtable的比较
Hashtable 和 HashMap的比较 1. HashMap可以接受null(HashMap可以接受为null的键值(key)和值(value), HashTable不可以接受为null的键( ...
- hashMap和hashTable的区别
每日总结,每天进步一点点 hashMap和hashTable的区别 1.父类:hashMap=>AbstractMap hashTable=>Dictionary 2.性能:hashMap ...
- SortedList和HashTable
都是集合类,C#中同属命名空间System.Collections,“用于处理和表现类似keyvalue的键值对,其中key通常可用来快速查找,同时key是区分大小写:value用于存储对应于key的 ...
- Java Hashtable的实现
先附源码: package java.util; import java.io.*; /** * This class implements a hash table, which maps keys ...
随机推荐
- DirectX 矩阵
基础: 下标:第一个下标为该元素所在行的索引,第二个下标为该元素所在列的索引.如下图所示 行向量和列向量:只有单行的向量称为行向量,只有单列的称之为列向量. 相等 维数和元素都相等 数乘(与标量相乘) ...
- eclipse创建java类时自动添加注释
一.按照以下顺序点击菜单 window-->preferences-->java-->Code Stype-->Code Templates-->Code-->Ne ...
- Task<TResult>的使用
https://msdn.microsoft.com/en-us/library/dd321424(v=vs.110).aspx Represents an asynchronous operatio ...
- Myeclipse 10.7 android(安卓) 开发环境搭建
1 下载并安装JDK,并且设置环境变量 2 下载安装 installer_r24.3.4-windows.exe (Android SDK Manager) 3 使用 Android SDK Mana ...
- linux bash shell 流程控制(if/else )
本文转自:http://blog.csdn.net/flowingflying/article/details/5069646 本文也即<Learning the bash Shell>3 ...
- Hibernate+Struts2+jsp 修改用户信息
在用户列表页面点击修改,进入修改页面 修改薪酬为555,点击提交,重新跳回该页面 修改成功 关键代码如下 基层的代码,这里增加了一个根据用户id查询的方法 dao层 //修改 public USer ...
- ARM寻址方式
寻址方式: 所谓寻址方式就是处理器根据指令中给出的信息来找到指令所需操作数的方式. 1.立即数寻址 2.寄存器寻址 3.寄存器间接寻址 就是寄存器中存放的是操作数在内存中的地址 例如以下指令: LDR ...
- Web开发, 跳转时出现java.lang.ClassNotFoundException
发生这种状况一般都是由于类找不到,要么是web.xml没有配对位置,要么是类没有放好
- Monocular Vision
Monocular Vision: a condition in which one eye is blind, seeing with only one eye Binocular Vision: ...
- SQL Server 2005 中的同义词
From : http://blog.csdn.net/itblog/article/details/752881 =============创建同义词 可以为下列对象类型创建同义词: 程序集 (CL ...