-
Notifications
You must be signed in to change notification settings - Fork 0
/
doublyLinkedHashTable.c
275 lines (227 loc) · 6.13 KB
/
doublyLinkedHashTable.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
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
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
#include <stdio.h>
#include <stdlib.h>
#define LOAD_FACTOR 3
typedef struct _listnode{
int key;
struct _listnode *next;
struct _listnode *pre;
} ListNode;
typedef struct _linkedlist{
int size;
ListNode *head;
} HashTableNode;
typedef struct _hashTable{
int hSize;
int nSize;
HashTableNode *Table;
} HashTable;
int Hash(int key,int hSize);
ListNode* HashSearch(HashTable, int);
int HashInsert(HashTable *, int);
int HashDelete(HashTable *, int);
//In Practice, we will not do it
void HashPrint(HashTable);
int main()
{
int opt;
int size;
int i;
int key;
//Create a HashTable
HashTable Q1;
printf("============= Hash Table ============\n");
printf("|1. Create a hash table |\n");
printf("|2. Insert a key to the hash table |\n");
printf("|3. Search a key in the hash table |\n");
printf("|4. Delete a key from the hash table|\n");
printf("|5. Print the hash table |\n");
printf("|6. Quit |\n");
printf("=====================================\n");
printf("Enter selection: ");
scanf("%d",&opt);
while(opt>=1 && opt <=5){
switch(opt){
case 1:
printf("Enter number of data to be inserted:\n");
scanf("%d",&size);
Q1.hSize = (int)size/ LOAD_FACTOR;
Q1.nSize = 0;
Q1.Table = (HashTableNode *) malloc(sizeof(HashTableNode)*(Q1.hSize));
for(i=0;i<Q1.hSize;i++){
Q1.Table[i].head = NULL;
Q1.Table[i].size = 0;
}
printf("HashTable is created.\n");
break;
case 2: //Insertion
printf("Enter a key to be inserted:\n");
scanf("%d",&key);
if(HashInsert(&Q1,key))
printf("%d is inserted.\n",key);
else
printf("%d is a duplicate. No key is inserted.\n",key);
break;
case 3: //Search
printf("Enter a key for searching in the HashTable:\n");
scanf("%d",&key);
ListNode* node = HashSearch(Q1, key);
if(node!=NULL)
printf("%d is found.\n",key);
else
printf("%d is not found.\n",key);
break;
case 4: //Deletion
printf("Enter a key to be deleted:\n");
scanf("%d",&key);
if(HashDelete(&Q1,key))
printf("%d is deleted.\n",key);
else
printf("%d is not existing.\n",key);
break;
case 5:
HashPrint(Q1);
break;
}
printf("Enter selection: ");
scanf("%d",&opt);
}
for(i=0;i<Q1.hSize;i++)
{
while(Q1.Table[i].head)
{
ListNode *temp;
temp = Q1.Table[i].head;
Q1.Table[i].head = Q1.Table[i].head->next;
free(temp);
}
}
free(Q1.Table);
return 0;
}
int HashInsert(HashTable* Q1Ptr, int key)
{
//Write your code here
int index, i;
//Get table and head
index = Hash(key, Q1Ptr->hSize);
HashTableNode cTNode = Q1Ptr->Table[index];
ListNode *cNode = cTNode.head;
//Create object for slot
ListNode * newNode = malloc(sizeof(ListNode));
newNode->key = key;
newNode->next = NULL;
if(cNode == NULL)
{
//Increase size
Q1Ptr->Table[index].size += 1;
newNode->pre = NULL;
Q1Ptr->Table[index].head = newNode;
return 1;
}
else
{
while(1)
{
//Return if duplicate
if(cNode->key == key)
{
free(newNode);
return 0;
}
//Add link
else if(cNode->next == NULL)
{
//Increase size
cTNode.size += 1;
//newNode->Head
Q1Ptr->Table[index].head->pre = newNode;
//NULL->newNode->HEAD
newNode->next = Q1Ptr->Table[index].head;
newNode->pre = NULL;
//Set head to new node
Q1Ptr->Table[index].head = newNode;
break;
}
cNode = cNode->next;
}
}
return 1;
}
int HashDelete(HashTable* Q1Ptr, int key)
{
//Write your code here
int index, i;
index = Hash(key, Q1Ptr->hSize);
HashTableNode cTNode = Q1Ptr->Table[index];
ListNode *cNode = cTNode.head;
ListNode *tempNode;
if(cNode == NULL)
return 0;
//If we're removing head
if(Q1Ptr->Table[index].head->key == key)
{ //Decrease size
cTNode.size -= 1;
tempNode = cNode;
cNode = cNode->next;
Q1Ptr->Table[index].head = cNode;
Q1Ptr->Table[index].head->pre = NULL;
free(tempNode);
return 1;
}
else
{
while(1){
//Not found
if(cNode == NULL)
return 0;
//Found
if(cNode->key == key)
{
//Decrease size
cTNode.size -= 1;
tempNode->next = cNode->next;
free(cNode);
return 1;
}
//Increment
tempNode = cNode;
cNode = cNode->next;
}
}
}
int Hash(int key,int hSize)
{
return key%hSize;
}
ListNode* HashSearch(HashTable Q1, int key)
{
int index;
ListNode *temp;
if(Q1.hSize != 0)
index = Hash(key,Q1.hSize);
else
return NULL;
temp = Q1.Table[index].head;
while(temp !=NULL){
if(temp->key == key)
return temp;
temp = temp->next;
}
return NULL;
}
void HashPrint(HashTable Q1)
{
int i;
ListNode *temp;
for(i=0;i<Q1.hSize;i++)
{
temp =Q1.Table[i].head;
printf("%d: ",i);
while(temp !=NULL)
{
printf("%d -> ", temp->key);
temp = temp->next;
}
printf("\n");
}
}