#include <stdio.h> #include <stdlib.h> #define M 13 typedef struct list { int key; struct list *next; }node,*link; node hashtab[M]; int h(int key) { return key%M; } void built(int key) { link m; link n; int index; n=(link)malloc(sizeof(node)); n->key=key; n->next=NULL; index=h(key); m=hashtab[index].next; if(m!=NULL) { n->next=m; hashtab[index].next=n; } else hashtab[index].next=n; } void output( int data[M]) { link q; int i; for(i=0;i<M;i++) { printf("hashtab[%d]",i); printf(" "); q= hashtab[i].next; while(q!=NULL) { if(q->key>0) printf("[%d] ",q->key); q=q->next; } printf("\n"); } } int find(int data[M],int key) { link q; int i; for(i=0;i<M;i++) { q= hashtab[i].next; while(q!=NULL) { if(q->key==key) { return 1; } q=q->next; } } return (-1); } void main() { int i,key,y; int data[M]; int index=0; printf("input data:"); for(i=M-2;i>=0;i--) scanf("%d",&data[i]); printf("\n"); while (index<M) { built(data[index]); index++; } output(data); printf("input search number:"); scanf("%d",&key); y=find(data,key); if(y>0) printf("%d is found",key); else printf("%d is not found",key); }
Powered by: 博客园 Copyright © 2024 little健健 Powered by .NET 9.0 on Kubernetes