-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathHashing-LinearProbing.c
79 lines (77 loc) · 1.65 KB
/
Hashing-LinearProbing.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
#include<stdlib.h>
#define SIZE 5
#include<stdio.h>
void insert(int);
void display();
void delete(int);
int htable[SIZE];
int main()
{
int ch,i,value;
for(i=0;i<SIZE;i++)
{
htable[i]=-1;//initializing the hash table to -1
}
while(1)
{
printf("ENTER CHOICE\n1:INSERT\n2:DISPLAY\n3:DELETE\n4:EXIT\n");
scanf("%d",&ch);
switch(ch)
{
case 1: printf("ENTER THE VALUE TO INSERT INTO HASH TABLE\n");
scanf("%d",&value);
insert(value);
break;
case 2: display();
break;
case 3: printf("ENTER A VALUE TO DELETE IN HASH TABLE\n");
scanf("%d",&value);
delete(value);
break;
case 4: exit(0);
}
}
return 0;
}
void insert(int value)
{
int index,i,flag=0;
for(i=0;i<SIZE;i++)
{ //Hash function.
/*h(x)=f(i) mod TABLE SIZE where f(i)=i */
index=(value+i)%SIZE;
if(htable[index]==-1)
{
htable[index]=value;
flag=1;
break;
}
}
if (flag==0)
{printf("ELEMENT CAN'T BE INSERTED\n");}
}
void delete(int value)
{
int index,i,flag=0;
for(i=0;i<SIZE;i++)
{
index=(value+i)%SIZE;
if(htable[index]==value)
{
htable[index]=-1;
printf("THE VALUE FOUND AT %d\n",index);
flag=1;
break;
}
}
if(flag==0)
{printf("VALUE NOT FOUND\n");}
}
void display()
{
int i;
printf("ELEMENTS IN HASH TABLE ARE\n");
for(i=0;i<SIZE;i++)
{
printf("AT INDEX %d VALUE=%d\n",i,htable[i]);}
}