-
Notifications
You must be signed in to change notification settings - Fork 1k
/
Open_hashing.c
214 lines (202 loc) · 4.2 KB
/
Open_hashing.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
/*
___Implement Open Hashing___
Hashing is an important data structure that is used to map a given value with a particular index
for faster access of elements. But sometimes more than one value gets mapped with a particular index.
This can be resolved using open hashing.
In open hashing, keys are stored in linked lists attached to cells of a hash table.
*/
#include<stdio.h>
#include<stdlib.h>
#define SIZE 10
struct node
{
int data;
struct node *next;
};
//array of pointers
struct node *head[SIZE]={NULL},*c;
void openDisplay();
void openInsert();
void openSearch();
void main()
{
//prints the hash function used in the program
printf("\nHash function : h(k)=k (mod 10)+1");
int size,i,temp,ch=0;
while(ch!=4)
{
printf("\n");
printf("1.Insert\n2.Search\n3.Display\n4.Exit\n");
printf("Enter your choice : ");
scanf("%d",&ch);
switch(ch)
{
case 1:
openInsert();
break;
case 2:
openSearch();
break;
case 3:
openDisplay();
break;
case 4:
break;
default:
printf("Invalid choice\n");
}
}
}
/*
This function inserts elements into the hash table.
Duplication is not allowed in hash table.
We use an array of linked list to store the elements.
*/
void openInsert()
{
int i,j,key;
printf("Enter the element : ");
scanf("%d",&key);
i=key%SIZE+1;
for(c=head[i];c!=NULL;c=c->next)
{
if(c->data==key) //checks for duplication
{
printf("Duplication\n");
return;
}
}
//dynamically allocates memory for the node
struct node *newnode=(struct node*)malloc(sizeof(struct node));
newnode->data=key;
newnode->next=NULL;
//if head is empty then element made as head
if(head[i]==NULL)
head[i]=newnode;
else
{
c=head[i];
while(c->next!=NULL)
c=c->next;
c->next=newnode;
}
}
/*
This function search for an element in the hash table.
The index corresponding to that particular value is found.
Search is made in the linked list whose head is at that index of the array.
*/
void openSearch()
{
int key,index;
printf("Enter the element : ");
scanf("%d",&key);
index=key%SIZE+1; //finds the index of the key value
//checks whether the head of the linked list is empty or not
if(head[index]==NULL)
printf("Element not found\n");
else
{
//traverse through the linked list
for(c=head[index];c!=NULL;c=c->next)
{
if(c->data==key)
{
printf("%d found at index %d\n",key,index);
break;
}
}
if(c==NULL)
printf("Element not found\n");
}
}
/*
This function displays elements in the hash table.
If no element is present the it displays "--" in the column value.
*/
void openDisplay()
{
int i;
printf("Index\tValue\n");
printf("---------------\n");
for(i=1;i<=SIZE;i++)
{
if(head[i]==NULL)
{
printf("%d\t--",i);
printf("\n");
continue;
}
else
{
c=head[i];
printf("%d\t%d",i,c->data);
c=c->next;
for(;c!=NULL;c=c->next)
printf("->%d",c->data);
}
printf("\n");
}
}
/*
Sample Input and Output:
Hash function : h(k)=k (mod 10)+1
1.Insert
2.Search
3.Display
4.Exit
Enter your choice
1
Enter the element : 44
1.Insert
2.Search
3.Display
4.Exit
Enter your choice
1
Enter the element : 14
1.Insert
2.Search
3.Display
4.Exit
Enter your choice : 1
Enter the element : 12
1.Insert
2.Search
3.Display
4.Exit
Enter your choice : 1
Enter the element : 89
1.Insert
2.Search
3.Display
4.Exit
Enter your choice : 1
Enter the element : 90
1.Insert
2.Search
3.Display
4.Exit
Enter your choice : 1
Enter the element : 11
1.Insert
2.Search
3.Display
4.Exit
Enter your choice : 3
Index Key
---------------
1 90
2 11
3 12
4 --
5 44->14
6 --
7 --
8 --
9 --
10 89
Time complexity :
Insertion - O(1)
Deletion - O(1)
*/