-
Notifications
You must be signed in to change notification settings - Fork 441
/
Copy path哈希查找.c
119 lines (97 loc) · 1.97 KB
/
哈希查找.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
#include<stdio.h>
#include<stdlib.h>
#define tablesize 5
typedef int ElemType;
typedef struct HashNode
{
ElemType elem;
struct HashNode *next;
}HashNode;
typedef struct
{
HashNode ChainHash[tablesize];
int count;
}HashTable;
int hash_mod(ElemType key)
{
return key % tablesize;
}
void InsertHash(HashTable *h, int key)
{
HashNode *p;
int index;
p = (HashNode*)malloc(sizeof(HashNode));
p->elem = key;
index = hash_mod(key);
p->next = h->ChainHash[index].next;
h->ChainHash[index].next = p;
h->count++;
}
void CreateHashTable(HashTable *h, int n)
{
int key;
int i;
for(i = 0; i < n; i++)
{
printf("Input the %d key :", i+1);
scanf_s("%d", &key);
InsertHash(h, key);
}
}
void PrintHashTable(HashTable *h)
{
int i;
HashNode *p;
for(i = 0;i <= tablesize; i++)
{
p = h->ChainHash[i].next;
while(p)
{
printf("%-5d", p->elem);
p = p->next;
}
}
}
int SearchHash(HashTable *h, int key)
{
HashNode *p;
int index;
int counter = 0;
index = hash_mod(key);
p = h->ChainHash[index].next;
while(p)
{
if(p->elem == key)
return 1;
else
p = p->next;
}
return 0;
}
void main()
{
int n ,key;
int i;
HashTable H;
printf("input the length of the Hash that we want to build:");
scanf_s("%d", &n);
for(i = 0;i <= tablesize; i++)
H.ChainHash[i].next = NULL;
H.count = 0;
CreateHashTable(&H,n);
printf("The hash table that we build is:");
PrintHashTable(&H);
printf("\nInput the key that we want to search(-1 for exit):");
scanf_s("%d", &key);
while(key != -1)
{
if(SearchHash(&H, key))
printf("There is a %d record in the Hash Table!\n", key);
else
printf("There is not a %d record in the Hash Table!\n", key);
printf("\nInput the key that we want to search(-1 for exit):");
scanf_s("%d", &key);
}
free(&H);
return;
}