-
Notifications
You must be signed in to change notification settings - Fork 0
/
coalescedHashing.c
177 lines (155 loc) · 4.33 KB
/
coalescedHashing.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
#include <stdio.h>
#include <stdlib.h>
#define TABLESIZE 37
#define PRIME 13
enum Marker {EMPTY,USED};
typedef struct _slot{
int key;
enum Marker indicator;
int next;
} HashSlot;
int HashInsert(int key, HashSlot hashTable[]);
int HashFind(int key, HashSlot hashTable[]);
int hash(int key)
{
return (key % TABLESIZE);
}
int main()
{
int opt;
int i;
int key;
int index;
HashSlot hashTable[TABLESIZE];
for(i=0;i<TABLESIZE;i++){
hashTable[i].next = -1;
hashTable[i].key = 0;
hashTable[i].indicator = EMPTY;
}
printf("============= Hash Table ============\n");
printf("|1. Insert a key to the hash table |\n");
printf("|2. Search a key in the hash table |\n");
printf("|3. Print the hash table |\n");
printf("|4. Quit |\n");
printf("=====================================\n");
printf("Enter selection: ");
scanf("%d",&opt);
while(opt>=1 && opt <=3){
switch(opt){
case 1:
printf("Enter a key to be inserted:\n");
scanf("%d",&key);
index = HashInsert(key,hashTable);
if(index <0)
printf("Duplicate key\n");
else if(index < TABLESIZE)
printf("Insert %d at index %d\n",key, index);
else
printf("Table is full.\n");
break;
case 2:
printf("Enter a key for searching in the HashTable:\n");
scanf("%d",&key);
index = HashFind(key, hashTable);
if(index!=-1)
printf("%d is found at index %d.\n",key,index);
else
printf("%d is not found.\n",key);
break;
case 3:
printf("index:\t key \t next\n");
for(i=0;i<TABLESIZE;i++) printf("%d\t%d\t%d\n",i, hashTable[i].key,hashTable[i].next);
break;
}
printf("Enter selection: ");
scanf("%d",&opt);
}
return 0;
}
H(int key, int i)
{
return hash(key+i);
}
int HashInsert(int key, HashSlot hashTable[])
{
// Write your code here
int index,i=0,prevIndex=-1,first_search=-1;
index = H(key,i);
while(1){
//Not used
if(hashTable[index].indicator == EMPTY)
{
//If linked then connect
if(prevIndex != -1)
{
hashTable[prevIndex].next = index;
}
//Store new key
hashTable[index].key = key;
hashTable[index].indicator = USED;
hashTable[index].next = -1;
break;
}
//If used
else if (hashTable[index].indicator == USED)
{
//if duplicate exist
if(hashTable[index].key == key)
return -1;
if(first_search == -1)
{
//keep a copy
prevIndex = index;
//Set to 0 to check if linked
first_search = 0;
}
}
i++;
//Increment index
if(hashTable[index].indicator == USED && hashTable[index].next != -1)
{
index = hashTable[index].next;
//If linked
if(first_search == 0)
{
//Set to -1 to check again if linked
first_search = -1;
}
}
else
{
index = H(key,i);
first_search = 1;
}
//Every slot has been searched
if(i == TABLESIZE)
return i;
}
return index;
}
int HashFind(int key, HashSlot hashTable[])
{
// Write your code here
int index, i=0, counter;
index = H(key,i);
while(1)
{
//When occupied compare keys
if(hashTable[index].indicator == USED && hashTable[index].key == key)
return index;
//Increment index
if(hashTable[index].indicator == USED && hashTable[index].next != -1)
{
i++;
index = hashTable[index].next;
}
else
{
i++;
index = H(key,i);
}
//Every slot has been searched
if(i == TABLESIZE)
return -1;
}
}