-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathlinear_probing.c
132 lines (123 loc) · 2.23 KB
/
linear_probing.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
#include<stdio.h>
#include<stdlib.h>
int a[100];
void insert(int key,int s)
{
int index=key%s;
if (a[index]==0)
{
a[index]=key;
printf("%d inserted",key);
}
else
{
int i=index, flag=0;
do
{
if(a[i]==0)
{
a[i]=key;
printf("%d inserted",key);
flag=1;
break;
}
i=(i+1)%s;
}while(i!=index);
if (flag==0)
{
printf("Hash Table is full");
exit(0);
}
}
}
void delete(int key,int s)
{
for(int i=0;i<s;i++)
{
if(a[i]==key)
{
a[i]=0;
printf("%d deleted",key);
}
}
}
void display(int s)
{
for(int i=0;i<s;i++)
{
if(a[i]==0)
{
printf("_\t");
}
else
{
printf("%d\t",a[i]);
}
}
}
void search(int key, int s)
{
int index= key%s;
if(a[index]==key)
{
printf("%d found at %d",key,index+1);
}
else
{
int i=index, flag=0;
do
{
if(a[i]==key)
{
printf("%d found at position %d",key,index+1);
flag=1;
break;
}
i=(i+1)%s;
} while(i!=index);
if (flag==0)
{
printf("Key not found");
}
}
}
void main()
{
int key;
int ch,s;
printf("Enter the size of hash table: ");
scanf("%d",&s);
for(int i=0;i<s;i++)
{
a[i]=0;
}
while(1)
{
printf("\nMenu\n1. Linear Probing\n2. Delete\n3. Search\n4. Display\n");
printf("Enter your choice: ");
scanf("%d",&ch);
switch(ch)
{
case 1:
printf("Enter key value: ");
scanf("%d",&key);
insert(key,s);
break;
case 2:
printf("Enter the value to be deleted: ");
scanf("%d",&key);
delete(key,s);
break;
case 3:
printf("Enter the value to be searched: ");
scanf("%d",&key);
search(key,s);
break;
case 4:
display(s);
break;
default:
printf("Enter valid option");
}
}
}