hash表初学
来源:互联网 发布:周岩的淘宝店叫什么 编辑:程序博客网 时间:2024/05/29 09:07
<经历了大概两周时间,重新捡起n年前学的C,其中也向木名先生的博客借鉴了一些,http://blog.csdn.net/jay_yin/article/details/40543507>
目的:实现Tomas文章中的实例(CPU下),http://www.adms-conf.org/2015/gpu-optimizer-camera-ready.pdf
代码:
#include <stdio.h>#include <malloc.h>#include<stdlib.h>typedef struct{int key; //hash addressint zip; //customer's zipcodeint count; //number of costomers with a same zipcodefloat sum; //total expr calculated}customer;typedef struct{customer *cus; //table pointer}hashtable;#define HL 30 //hashtable length//--------------------------------------------------------////initialize hashtableint inithash(hashtable *p,int m){int i;p->cus=malloc(m*sizeof(customer));if (!p->cus){printf("initialize failed!\n");return 1;}for (i=0;i<m;i++){p->cus[i].key=m+1;p->cus[i].zip=-1;p->cus[i].count=0;p->cus[i].sum=0;// printf("i=%d,key=%d,zip=%d\n",i,p->cus[i].key,p->cus[i].zip);// printf("count=%d,sum=%f\n",p->cus[i].count,p->cus[i].sum);}return 0;}int hashfunc(int key){return key%HL;}// opening addressing//int addhash(hashtable *p,int zipcode,float originprice,float tax){int addr;float expr;addr=hashfunc(zipcode);expr=(originprice-tax)/originprice;while((p->cus[addr].zip!=zipcode) && (p->cus[addr].key!=HL+1)){addr=hashfunc(addr+1);if (addr=hashfunc(zipcode)){printf("cannot insert this zipcode!\n");return 1;}}p->cus[addr].key = addr;p->cus[addr].zip = zipcode;p->cus[addr].count+= 1;p->cus[addr].sum += expr;// printf("addr=%d,key=%d,zip=%d\n",addr,p->cus[addr].zip,p->cus[addr].zip);// printf("count=%d,sum=%f\n",p->cus[addr].count,p->cus[addr].sum);return 0;}int searchash(hashtable *p, int zipcode){int addr;addr=hashfunc(zipcode);// printf(addr);while ((p->cus[addr].zip!=zipcode) && (p->cus[addr].key!=HL+1)){addr=(addr+1)%HL;if (addr==hashfunc(zipcode)){// printf("addr=%f,zipcode=%f",addr,zipcode);printf("cannot find the zip code!\n");return 1;}}if (p->cus[addr].key==HL+1){printf("cannot find the zip code!\n");return 1;}if (p->cus[addr].zip==zipcode){printf("this zipcode has been found!\n");return addr;}}void destroyhash(hashtable *p){ free(p->cus); p->cus=NULL;}int main(){int sym;int inputnum=1;hashtable pT;int czip;float cprice,ctax;sym=inithash(&pT, HL);if (sym){exit(0);}while(inputnum){printf("please choose the following num:\n");printf("1-add; 2-search; 3-exit\n");printf("--------------------------------\n");scanf("%d",&inputnum);switch(inputnum){case 1:{printf("please input zip,originprice,tax:\n");scanf("%i%f%f",&czip,&cprice,&ctax);sym=addhash(&pT,czip,cprice,ctax);break;if (sym){exit(0);}}case 2:{float summ,countt;printf("please input the zip you want to query:\n");scanf("%d",&czip);sym=searchash(&pT,czip);if (sym==1){break;}else{countt=pT.cus[sym].count;summ=pT.cus[sym].sum;printf("the num of orders in this zip is:%f\n",countt);printf("the average discount in this zip is:%f\n",(summ/countt));break;}}case 3:{destroyhash(&pT);exit(0);}default:{printf("please choose a right number!\n");printf("-------------------------------\n");}}}return 0;}
代码还有些粗糙,第一次写C不易,且写且珍惜~
1 0
- hash表初学
- [数据结构]Hash表初学(数组链表)
- [数据结构]Hash表初学(开放寻址法 )
- boost::hash初学乍练,呵呵
- 初学Scala(1): Scala实现Hash Join
- Hash 函数、Hash表
- hash表、hash算法
- hash-A-hash表
- Hash表、Hash冲突
- hash表
- hash表
- hash表
- hash表
- hash表
- hash表
- Hash表
- hash表
- hash表
- java Servlet 实现解析Excel
- MySQL之联结(join)操作类型概念总结
- 第十八章 什么是 daemon 与服务 (service)
- 第十八章 解析 super daemon 的配置文件
- 第十八章 服务的防火墙管理 xinetd, TCP Wrappers
- hash表初学
- 第十八章 系统开启的服务
- Python函数的各种参数用法(含星号参数)
- oracle merge 更新或插入数据
- Android Binder 全解析(3) -- AIDL原理剖析
- js中几种实用的跨域方法原理详解
- 第十九章 什么是登录文件
- maven 命令创建web项目和jetty插件
- expdp报错:ora-39127“WMSYS”