forked from DHEERAJHARODE/Hacktoberfest2024-Open-source-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhashing_linear_probing.c
138 lines (128 loc) · 2.58 KB
/
hashing_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
133
134
135
136
137
138
/* This code is contributed by Ayush Raj (https://github.com/Ayush0751)
This code deals with different operations in hashing using the method called linear probing, which includes
1. Insertion
2. Deletion
3. Searching
4. Displaying
Note: Here the max no of element to be inserted is taken as 10. It can be changed by increasing size to the desired integer.
*/
#include <stdio.h>
#include <stdlib.h>
int size = 10;
void insert(int arr[])
{
int num, i, count = 0;
printf("enter the data to insert\n");
scanf("%d", &num);
i = num % size;
while (arr[i] != -1)
{
i = ((i + 1) % 10);
}
if (arr[i == -1])
{
arr[i] = num;
}
else
{
printf("hash table is full\n");
}
}
void search(int arr[])
{
int num, i, count = 0;
printf("enter the number to search\n");
scanf("%d", &num);
i = num % size;
while (arr[i] != num)
{
if (count > 10)
{
break;
}
i = ((i + 1) % 10);
count++;
}
if (arr[i] == num)
{
printf("%d found at index %d\n", num, i);
}
else
{
printf("%d not found\n", num);
}
}
void delete (int arr[])
{
int num, i, count = 0;
printf("enter the number to delete\n");
scanf("%d", &num);
i = num % size;
while (arr[i] != num)
{
if (count > 10)
{
break;
}
i = ((i + 1) % 10);
count++;
}
if (arr[i] == num)
{
arr[i] = -1;
printf("%d deleted at index %d\n", num, i);
}
else
{
printf("%d not found\n", num);
}
}
void display(int arr[])
{
for (int i = 0; i < size; i++)
{
if (arr[i] != -1)
printf("%d is present at index %d\n", arr[i], i);
}
}
void initialize(int arr[])
{
for (int i = 0; i < size; i++)
{
arr[i] = -1;
}
}
int main()
{
int arr[10];
int i, j, choice;
initialize(arr);
while (1)
{
printf("1. Insert\n2. Search\n3. Delete\n4. Display\n5. Exit\n");
printf("enter your choice\n");
scanf("%d", &choice);
switch (choice)
{
case 1:
insert(arr);
break;
case 2:
search(arr);
break;
case 3:
delete (arr);
break;
case 4:
display(arr);
break;
case 5:
printf("Exitting...\n");
exit(0);
default:
printf("Wrong choice entered\n");
printf("Enter from the available choices only\n\n");
break;
}
}
}