-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLab12.c
More file actions
79 lines (77 loc) · 1.66 KB
/
Copy pathLab12.c
File metadata and controls
79 lines (77 loc) · 1.66 KB
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
/*
Given a File of N employee records with a set K of Keys(4-digit) which uniquely
determine therecords in file F. Assume that file F is maintained in memory by a
Hash Table(HT) of m memory
locations with L as the set of memory addresses (2- digit) of locations in HT.
Let the keys in K and addresses in L are Integers. Design and develop a Program
in C that uses Hash function H:
K → L as H(K)=K mod m (remainder method), and implement hashing technique to map
a given key K to the address space L. Resolve the collision (if any) using
linear probing.
*/
#include<stdio.h>
#include<stdlib.h>
int key,n,m,*ht,hi,elec,flag;
void createht()
{
int i;
ht = (int*)malloc(m*sizeof(int));
if(m==0)
{
printf("Unable to create the hash table\n");
exit(0);
}
else
for(i=0; i<m; i++)
ht[i] = -1;
}
void insertht(int key)
{
hi = key % m;
while(ht[hi] != -1)
{
hi = (hi+1)%m;
flag = 1;
}
if(flag)
{
printf("Collision Detected and avoided by Linear Probing!\n");
flag = 0;
}
ht[hi] = key;
elec++;
}
void displayht()
{
int i;
if(elec == 0)
{
printf("Hash Table is empty\n");
return;
}
printf("Hash Table contents are:\n");
for(i=0; i<m; i++)
printf("[%d] --> %d\n", i, ht[i]);
}
void main()
{
int i;
printf("\n%d\n",elec);
printf("Enter the number of employee records: ");
scanf("%d", &n);
printf("Enter the two digit memory locations: ");
scanf("%d", &m);
createht();
printf("Enter four digit key values of Employee records\n");
for(i=0; i<n; i++)
{
scanf("%d", &key);
if(elec == m)
{
printf("Hash table is full.\n");
break;
}
insertht(key);
}
displayht();
}