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 {
22int ID;
23char *name;
24float 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 {
67printf("ID : %d \t Name: %s\t Salary:%f\n",
68employee->ID, employee->name, employee->salary);
69}
70
71 int main( int argc, char * argv[])
72 {
73int size;
74int key1;
75static char ch;
76/**//* 员工资料数组 */
77DataType 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/**//* 数组元素个数 */
86size = sizeof(employee) / sizeof(employee[0]);
87
88/**//* 将员工资料数组映射到哈希表 */
89Create_Hash(employee, size);
90
91/**//* 输入一个员工的ID,查找并显示相关信息 */
92printf("请输入一位员工的ID:\n");
93scanf("%d", &key1);
94HashSearch(key1);
95}
96
97 void Create_Hash(DataType * employees, int size)
98 {
99int i, j;
100DataType empty = {0, NULL, 0.0};
101for (i = 0; i < HASHSIZE; i++)
102{
103Hash[i] = empty;
104}
105for (i = 0; i < size; i++)
106{
107j = 0;
108while (j < HASHSIZE)
109{
110/**//* 根据员工ID,将员工资料存放到哈表 */
111if (Hash[(employees[i].ID % HASHSIZE) + j].ID == 0)
112{
113Hash[(employees[i].ID % HASHSIZE) + j] = employees[i];
114break;
115}
116/**//* j++表示发生了冲突 */
117else
118j++;
119}
120}
121}
122
123 int HashFun( int key)
124 {
125return key % HASHSIZE;
126}
127
128 DataType HashValue( int key)
129 {
130return Hash[key % HASHSIZE];
131}
132
133 int HashSearch( int key)
134 {
135DataType temp;
136int address, count = 0;
137address = HashFun(key);
138count++;
139temp = HashValue(address);
140if (temp.ID == key)
141{
142printemployee(&temp);
143return 1;
144}
145else if (temp.ID == 0)
146{
147printf("没有找到与您输入ID相关的记录!\n");
148return 0;
149}
150else
151{
152while (count < HASHSIZE)
153{
154address = OverHandle(address);
155temp = HashValue(address);
156if (temp.ID == key)
157{
158printemployee(&temp);
159return 1;
160}
161count++;
162}
163}
164return 0;
165}
166
167 int OverHandle( int address)
168 {
169return (address+1) % HASHSIZE;
170}
171
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 {
22int ID;
23char *name;
24float 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 {
67printf("ID : %d \t Name: %s\t Salary:%f\n",
68employee->ID, employee->name, employee->salary);
69}
70
71 int main( int argc, char * argv[])
72 {
73int size;
74int key1;
75static char ch;
76/**//* 员工资料数组 */
77DataType 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/**//* 数组元素个数 */
86size = sizeof(employee) / sizeof(employee[0]);
87
88/**//* 将员工资料数组映射到哈希表 */
89Create_Hash(employee, size);
90
91/**//* 输入一个员工的ID,查找并显示相关信息 */
92printf("请输入一位员工的ID:\n");
93scanf("%d", &key1);
94HashSearch(key1);
95}
96
97 void Create_Hash(DataType * employees, int size)
98 {
99int i, j;
100DataType empty = {0, NULL, 0.0};
101for (i = 0; i < HASHSIZE; i++)
102{
103Hash[i] = empty;
104}
105for (i = 0; i < size; i++)
106{
107j = 0;
108while (j < HASHSIZE)
109{
110/**//* 根据员工ID,将员工资料存放到哈表 */
111if (Hash[(employees[i].ID % HASHSIZE) + j].ID == 0)
112{
113Hash[(employees[i].ID % HASHSIZE) + j] = employees[i];
114break;
115}
116/**//* j++表示发生了冲突 */
117else
118j++;
119}
120}
121}
122
123 int HashFun( int key)
124 {
125return key % HASHSIZE;
126}
127
128 DataType HashValue( int key)
129 {
130return Hash[key % HASHSIZE];
131}
132
133 int HashSearch( int key)
134 {
135DataType temp;
136int address, count = 0;
137address = HashFun(key);
138count++;
139temp = HashValue(address);
140if (temp.ID == key)
141{
142printemployee(&temp);
143return 1;
144}
145else if (temp.ID == 0)
146{
147printf("没有找到与您输入ID相关的记录!\n");
148return 0;
149}
150else
151{
152while (count < HASHSIZE)
153{
154address = OverHandle(address);
155temp = HashValue(address);
156if (temp.ID == key)
157{
158printemployee(&temp);
159return 1;
160}
161count++;
162}
163}
164return 0;
165}
166
167 int OverHandle( int address)
168 {
169return (address+1) % HASHSIZE;
170}
171