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