-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathHashing-LinearProbing.c
81 lines (75 loc) · 1.61 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
80
81
#include<stdio.h>
#include<stdlib.h>
#define size 100
int hashtable[size], n;
int hash(int key)
{
return (key % n);
}
void insert(int x)
{
int index,start;
index = hash(x);
start = index;
while(hashtable[index] != -1)
{
if(hashtable[index] == -1)
break;
index = (index + 1) % n;
if(index == start)
{
printf("Hash Table is full.\n");
return;
}
}
hashtable[index] = x;
}
void search(int x)
{
int index, start;
index = hash(x);
start = index;
while(hashtable[index] != x)
{
if(hashtable[index] == x)
break;
index = (index + 1) % n;
if(index == start)
{
printf("Element not found.\n");
return;
}
}
printf("Element found at %d index\n", index);
}
void display()
{
for(int i = 0; i < n; i++)
{
printf("At index %d : %d\n", i, hashtable[i]);
}
}
void main()
{
int x, op, i = 0;
printf("Enter table size : ");
scanf("%d", &n);
for(i = 0; i < n; i++)
hashtable[i] = -1;
while(1)
{
printf("1.Insert\t2.Display\t3.Search\t4.Exit\n");
printf("Enter your choice : ");
scanf("%d", &op);
switch(op)
{
case 1: printf("Enter an element to be inserted : ");
scanf("%d", &x);
insert(x); display(); break;
case 2: display(); break;
case 3: printf("Enter an element to search : ");
scanf("%d", &x); search(x); break;
case 4: exit(0);
}
}
}