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 ...
随机推荐
- 各种操作中心Operation Center一览
Operation Center在中国可能有很多种名称,例如指挥中心.运维室.总控中心等等,国外可能也有很多名称,不管名称如何,任何一个上规模得数据总心或者运维单位一般都有一个这样得中心,来负责所管理 ...
- 读取excel到数据库里面
//读取excel数据到dataTable里面 public DataTable ReadExcelDataToDataTable(string path) { DataTable dt = new ...
- JAVA开发--U盘EXE恢复工具
原理比较简单,在学校机房U盘总被感染,写一个工具来方便用 package com.udiskrecover; import java.awt.Container; import java.awt.Fl ...
- 在Web应用和IntelliJ IDEA中使用Spring框架
在JAVA SE和Web应用中都可以使用Spring, 这里只说在Web程序中的应用. 下面将以Spring 3.0.5版本为例. 在Web中使用Spring只需要如下两个步骤: 第一,将Spring ...
- poj 1696 Space Ant (极角排序)
链接:http://poj.org/problem?id=1696 Space Ant Time Limit: 1000MS Memory Limit: 10000K Total Submissi ...
- 序列的方法(str,list,tuple)
作者:Vamei 出处:http://www.cnblogs.com/vamei 欢迎转载,也请保留这段声明.谢谢! 在快速教程中,我们了解了最基本的序列(sequence).回忆一下,序列包含有定值 ...
- 2----lua的入门
Lua的标识符 什么是标识符? 标识符就是你给对象,函数等取的名字 区分大小写 首字母可以使字母下划线和美元符号组成 Lua 的保留字 保留字(区分大小写) true false and or not ...
- flex布局注意点:
1.父元素display:flex之后成为伸缩容器,子元素(除了position:absolute或fixed)无论是display:block或者display:inline,都成为了伸缩项目.2. ...
- Xstream 学习地址
http://forestqqqq.iteye.com/category/301129
- eclipse 安装git
1.安装Git 首先安装Git.这里只讲Windows环境下安装Git方法. 从Git下载git的Windows安装文件,一路Next到选择安装组件这一步: 选上Git Bash Here这一项,这样 ...