forked from widora/ctest
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhash.c
More file actions
executable file
·187 lines (151 loc) · 4.79 KB
/
hash.c
File metadata and controls
executable file
·187 lines (151 loc) · 4.79 KB
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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
#include <stdlib.h> //malloc()
#include <string.h> //memset()
#include <stdio.h>
#include <stdbool.h>
#include <signal.h> //signal()
#define HASH_TABLE_SIZE 10
typedef struct _NODE //--------- define a data link
{
int data; //-------------------- !!!!!!! data is a key word !!!!!!!
//--- put more data here ---//
struct _NODE* next;
}NODE; //-----!!!!!!!! end of struct token ;;;;;;;;; !!!!!
typedef struct _HASH_TABLE
{
NODE* HashTbl_item[HASH_TABLE_SIZE]; //--------- hash table with total HASH_TABLE_SIZE HashTbl_items (links)
}HASH_TABLE;
HASH_TABLE* create_hash_table() //---- create a hash table
{
HASH_TABLE* pHashTbl=(HASH_TABLE*)malloc(sizeof(HASH_TABLE));
memset(pHashTbl,0,sizeof(HASH_TABLE));
return pHashTbl;
}
//---- find data in which link node -----
NODE* find_data_in_hash(HASH_TABLE* pHashTbl,int data)
{
NODE* pNode;
if(NULL == pHashTbl) //--hash table is null
return NULL;
if(NULL == (pNode=pHashTbl->HashTbl_item[data % HASH_TABLE_SIZE])) //--HashTbl_item(link node) is null
return NULL;
while(pNode) //---while pNode is not null, search each link node for the data
{
if(data == pNode->data)
return pNode;
pNode=pNode->next;
}
return NULL;
}
//================== insert data into hash ===================
bool insert_data_into_hash(HASH_TABLE* pHashTbl,int data)
{
NODE* pNode;
if(NULL == pHashTbl) //---hash table is null
return false;
if(NULL == pHashTbl->HashTbl_item[data % HASH_TABLE_SIZE]) //--corresponding HashTbl_item (link node) is null
{
pNode=(NODE*)malloc(sizeof(NODE));
memset(pNode,0,sizeof(NODE));
pNode->data = data;
pHashTbl->HashTbl_item[data % HASH_TABLE_SIZE]=pNode;
return true;
}
if(NULL!= find_data_in_hash(pHashTbl,data)) //--if data exist
return false;
pNode=pHashTbl->HashTbl_item[data % HASH_TABLE_SIZE]; //-- data hash to an alread existed HashTbl_item
while(NULL!=pNode->next)
pNode=pNode->next; //---move to end of the link
//---add a link node and put data into
pNode->next=(NODE*)malloc(sizeof(NODE));
memset(pNode->next,0,sizeof(NODE));
pNode->next->data =data;
return true;
}
//===================== delete data from hash ======================
bool delete_data_from_hash(HASH_TABLE* pHashTbl,int data)
{
NODE* pHead;
NODE* pNode;
if(NULL==pHashTbl || NULL==pHashTbl->HashTbl_item[data%HASH_TABLE_SIZE]) //--hash table or HashTbl_item doesn't exist
return false;
if(NULL==(pNode=find_data_in_hash(pHashTbl,data))) //--the data haven't been hashed in before
return false;
if(pNode==pHashTbl->HashTbl_item[data%HASH_TABLE_SIZE]) //--if it's the first node in found hash table HashTbl_item
{
pHashTbl->HashTbl_item[data%HASH_TABLE_SIZE]=pNode->next; //--adjust the head node for hash HashTbl_item, prepare for deleting.
goto final;
}
pHead=pHashTbl->HashTbl_item[data%HASH_TABLE_SIZE];
while(pNode!=pHead->next)
pHead=pHead->next;
pHead->next=pNode->next; //--bridge two pnodes beside the wanted pnode,preapare for deleting .
final:
free(pNode); //--delete the pnode
return true;
}
int count_hash_data(HASH_TABLE* pHashTbl)
{
int i,cnt=0;
NODE *pTemp;
for(i=0;i<HASH_TABLE_SIZE;i++)
{
if(pHashTbl->HashTbl_item[i]!=NULL) //
{
cnt++;
pTemp=pHashTbl->HashTbl_item[i];
while(pTemp->next!=NULL)
{
pTemp=pTemp->next;
cnt++;
}//-while
}//-if
}//-for
return cnt;
}
void release_hash_table(HASH_TABLE* pHashTbl)
{
int i;
NODE *pHead,*pTemp;
for(i=0;i<HASH_TABLE_SIZE;i++)
{
if(pHashTbl->HashTbl_item[i]!=NULL) //
{
printf("------- Free hash table item[%d] -------\n",i);
pHead=pHashTbl->HashTbl_item[i];
while(pHead)
{
pTemp=pHead; //---two pnodes, one for deleting and one for pointing.
pHead=pHead->next;
if(pTemp!=NULL)
{
printf("free a node with data=%d! \n",pTemp->data);
free(pTemp);
}
}//-end of while
}//-end of if
}//-end of for
}
//-------- global variables for signal handler --------
HASH_TABLE* pHashTbl_CODE;
void sighandler(int sig)
{
printf("Signal to exit......\n");
release_hash_table(pHashTbl_CODE);
exit(0);
}
//=========================== main =================================
void main(void)
{
int i;
//HASH_TABLE* pHashTbl_CODE;
pHashTbl_CODE=create_hash_table(); //--INIT HASH TABLE
//signal handle
signal(SIGINT,sighandler);
for(i=0;i<999;i++)
insert_data_into_hash(pHashTbl_CODE,i);
printf("total hashed data number:%d \n",count_hash_data(pHashTbl_CODE));
while(1)sleep(200000);
printf("----- HASH TABLE EXAMPLE -----\n");
release_hash_table(pHashTbl_CODE);
//--------free mem-------
}