HashSearch 的实现
下面这段代码的哈希实现体我觉得有不是很托的地方,哈希表建立最好只存放键值,不用把关键词结构也放进去,因为哈希表大小一般为关键词表的很多倍,为了散列效果好,你必须吧键值表高的大点以实现很好的散列。这里的程序把关键词也放进哈希表中但是却没用,我觉得不是很好的实现方式,分开,做成两个不同的表,实现效果可能更好~
1
/*
2
* Copyright (c) 2005 All rights reserved.
3
* 文件名:HashSearch.c
4
*
5
* 文件标识:HashSearch
6
* 摘要:一个简单的哈希表搜索示例
7
* 输入:员工的ID
8
* 输出:根据输入的ID查找员工资料并输出,若没有找到给出相应提示
9
*
10
* 当前版本 0.01
11
* 作者:罗
12
* 完成日期:2006年3月28日
13
*/
14
15
#include <stdio.h>
16
#include <stdlib.h>
17
#define HASHSIZE 11
18
19
/* 哈希表元素的结构定义 */
20
typedef struct
21
{
22
int ID;
23
char *name;
24
float salary;
25
} employee;
26
27
typedef employee DataType;
28
29
/* 定义一个全局的,元素类型为 employee 的哈希表 */
30
DataType Hash[HASHSIZE];
31
32
/*
33
函数名:Create_Hash
34
参数:employees为员工资料数组, size为数组大小
35
功能:将大小为size的员工资料数组按员工ID映射到Hash表
36
*/
37
void Create_Hash(DataType *employees, int size);
38
39
/*函数名:HashFun
40
参数:key为员工ID
41
功能:将员工ID映射为Hash表中的下标地址
42
返回值:返回给定关键字对应的Hash表下标地址
43
*/
44
int HashFun(int key);
45
46
/*函数名:HashSearch
47
参数:key为员工的ID
48
功能:在Hash表中搜索给定关键字的员工信息
49
返回值:找到返回1,并输出员工资料, 找不到返回0并提示没有找到
50
*/
51
int HashSearch(int key);
52
53
/*函数名:OverHandle
54
参数:address发生冲突Hash表下标地址
55
功能:
56
返回值:
57
*/
58
int OverHandle(int address);
59
60
/*函数名:printemployee
61
参数:一个员工资料结构的指针
62
功能:屏幕输出员工资料
63
返回值:无
64
*/
65
void printemployee(DataType *employee)
66
{
67
printf("ID : %d \t Name: %s\t Salary:%f\n",
68
employee->ID, employee->name, employee->salary);
69
}
70
71
int main(int argc, char* argv[])
72
{
73
int size;
74
int key1;
75
static char ch;
76
/* 员工资料数组 */
77
DataType employee[] = {
78
{11, "luojiafeng", 5000},
79
{37, "wangqian", 8000},
80
{48, "liujie", 6000},
81
{97, "gaoxing", 10000},
82
{86, "xiaozhen", 6000},
83
{26, "chenghu", 8800}
84
};
85
/* 数组元素个数 */
86
size = sizeof(employee) / sizeof(employee[0]);
87
88
/* 将员工资料数组映射到哈希表 */
89
Create_Hash(employee, size);
90
91
/* 输入一个员工的ID,查找并显示相关信息 */
92
printf("请输入一位员工的ID:\n");
93
scanf("%d", &key1);
94
HashSearch(key1);
95
}
96
97
void Create_Hash(DataType *employees, int size)
98
{
99
int i, j;
100
DataType empty = {0, NULL, 0.0};
101
for (i = 0; i < HASHSIZE; i++)
102
{
103
Hash[i] = empty;
104
}
105
for (i = 0; i < size; i++)
106
{
107
j = 0;
108
while (j < HASHSIZE)
109
{
110
/* 根据员工ID,将员工资料存放到哈表 */
111
if (Hash[(employees[i].ID % HASHSIZE) + j].ID == 0)
112
{
113
Hash[(employees[i].ID % HASHSIZE) + j] = employees[i];
114
break;
115
}
116
/* j++表示发生了冲突 */
117
else
118
j++;
119
}
120
}
121
}
122
123
int HashFun(int key)
124
{
125
return key % HASHSIZE;
126
}
127
128
DataType HashValue(int key)
129
{
130
return Hash[key % HASHSIZE];
131
}
132
133
int HashSearch(int key)
134
{
135
DataType temp;
136
int address, count = 0;
137
address = HashFun(key);
138
count++;
139
temp = HashValue(address);
140
if (temp.ID == key)
141
{
142
printemployee(&temp);
143
return 1;
144
}
145
else if (temp.ID == 0)
146
{
147
printf("没有找到与您输入ID相关的记录!\n");
148
return 0;
149
}
150
else
151
{
152
while (count < HASHSIZE)
153
{
154
address = OverHandle(address);
155
temp = HashValue(address);
156
if (temp.ID == key)
157
{
158
printemployee(&temp);
159
return 1;
160
}
161
count++;
162
}
163
}
164
return 0;
165
}
166
167
int OverHandle(int address)
168
{
169
return (address+1) % HASHSIZE;
170
}
171
/*2
* Copyright (c) 2005 All rights reserved.3
* 文件名:HashSearch.c4
*5
* 文件标识:HashSearch6
* 摘要:一个简单的哈希表搜索示例7
* 输入:员工的ID8
* 输出:根据输入的ID查找员工资料并输出,若没有找到给出相应提示9
*10
* 当前版本 0.0111
* 作者:罗12
* 完成日期:2006年3月28日13
*/14

15
#include <stdio.h>16
#include <stdlib.h>17
#define HASHSIZE 1118

19
/* 哈希表元素的结构定义 */20
typedef struct21
{22
int ID;23
char *name;24
float salary;25
} employee;26

27
typedef employee DataType;28

29
/* 定义一个全局的,元素类型为 employee 的哈希表 */30
DataType Hash[HASHSIZE];31

32
/*33
函数名:Create_Hash34
参数:employees为员工资料数组, size为数组大小35
功能:将大小为size的员工资料数组按员工ID映射到Hash表36
*/37
void Create_Hash(DataType *employees, int size);38

39
/*函数名:HashFun40
参数:key为员工ID41
功能:将员工ID映射为Hash表中的下标地址42
返回值:返回给定关键字对应的Hash表下标地址43
*/44
int HashFun(int key);45

46
/*函数名:HashSearch47
参数:key为员工的ID48
功能:在Hash表中搜索给定关键字的员工信息49
返回值:找到返回1,并输出员工资料, 找不到返回0并提示没有找到50
*/51
int HashSearch(int key);52

53
/*函数名:OverHandle54
参数:address发生冲突Hash表下标地址55
功能:56
返回值:57
*/58
int OverHandle(int address);59

60
/*函数名:printemployee61
参数:一个员工资料结构的指针62
功能:屏幕输出员工资料63
返回值:无64
*/65
void printemployee(DataType *employee)66
{67
printf("ID : %d \t Name: %s\t Salary:%f\n",68
employee->ID, employee->name, employee->salary);69
}70

71
int main(int argc, char* argv[])72
{73
int size;74
int key1;75
static char ch;76
/* 员工资料数组 */77
DataType employee[] = {78
{11, "luojiafeng", 5000},79
{37, "wangqian", 8000},80
{48, "liujie", 6000},81
{97, "gaoxing", 10000},82
{86, "xiaozhen", 6000},83
{26, "chenghu", 8800}84
};85
/* 数组元素个数 */86
size = sizeof(employee) / sizeof(employee[0]);87

88
/* 将员工资料数组映射到哈希表 */89
Create_Hash(employee, size);90

91
/* 输入一个员工的ID,查找并显示相关信息 */92
printf("请输入一位员工的ID:\n");93
scanf("%d", &key1);94
HashSearch(key1);95
}96

97
void Create_Hash(DataType *employees, int size)98
{99
int i, j;100
DataType empty = {0, NULL, 0.0};101
for (i = 0; i < HASHSIZE; i++)102
{103
Hash[i] = empty;104
}105
for (i = 0; i < size; i++)106
{107
j = 0;108
while (j < HASHSIZE)109
{110
/* 根据员工ID,将员工资料存放到哈表 */111
if (Hash[(employees[i].ID % HASHSIZE) + j].ID == 0)112
{113
Hash[(employees[i].ID % HASHSIZE) + j] = employees[i];114
break;115
}116
/* j++表示发生了冲突 */117
else118
j++;119
}120
}121
}122

123
int HashFun(int key)124
{125
return key % HASHSIZE;126
}127

128
DataType HashValue(int key)129
{130
return Hash[key % HASHSIZE];131
}132

133
int HashSearch(int key)134
{135
DataType temp;136
int address, count = 0;137
address = HashFun(key);138
count++;139
temp = HashValue(address);140
if (temp.ID == key)141
{142
printemployee(&temp);143
return 1;144
}145
else if (temp.ID == 0)146
{147
printf("没有找到与您输入ID相关的记录!\n");148
return 0;149
}150
else151
{152
while (count < HASHSIZE)153
{154
address = OverHandle(address);155
temp = HashValue(address);156
if (temp.ID == key)157
{158
printemployee(&temp);159
return 1;160
}161
count++;162
}163
}164
return 0;165
}166

167
int OverHandle(int address)168
{169
return (address+1) % HASHSIZE;170
}171




浙公网安备 33010602011771号