哈希表查找

来源:互联网 发布:百能抽奖软件 编辑:程序博客网 时间:2024/05/22 06:58
/**************************  哈希表查找  ***********************/#include<stdio.h>#include<stdlib.h>#define size 15#define mod 15typedef int datatype;typedef struct node{datatype data;struct node *next;}*list,node;void build(int tmp[],int i,list *p){  list new=(list)malloc(sizeof(struct node));new->data = tmp[i];new->next = NULL;if(*(p+tmp[i]%mod) == NULL){*(p+tmp[i]%mod) = new;return ;}else {list q = *(p+tmp[i]%mod);while((q->next) != NULL)q = q->next;q->next = new;}}void find(list *p,int x){if(*(p+x%mod) != NULL){ if((*(p+x%mod))->data == x)printf("find the data p[%d]->%d\n",x%mod,((*(p+x%mod))->data));else{list tmp;tmp = *(p+x%mod);while(tmp != NULL)if(tmp->data == x){printf("find the data p[%d]->%d\n",x%mod,tmp->data);return ;}elsetmp=tmp->next;printf("sorry ,the data is not exist\n");//while( tmp->data != x)//tmp = tmp->next;//printf("find the data p[%d]->%d\n",x%mod,tmp->data);}}else{printf("sorry,the data is not exist\n");return ;}}int main(void){int  a[15]={1,1,2,3,5,8,13,21,34,55,89,144,233,377,610}; static   struct node *p[15];int i=0;while(i < size){build(a,i,p); //  建立哈希表存储数据.i++;}printf("哈希表查找\n");int tmp;printf("please input the data you want to find\n");if(scanf("%d",&tmp) != 1) return -1;find(p,tmp);printf("\n");return 0;}


原创粉丝点击