0% found this document useful (0 votes)
4 views

PROGRAM 12

Uploaded by

priyapoojary1611
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views

PROGRAM 12

Uploaded by

priyapoojary1611
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

PROGRAM 12:

Given a File of N employee records with a set K of Keys(4-digit)


which uniquely determine the records 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 * ht, n, m;
int count = 0;
void insert(int k) {
int currentIndex = k % m;
while (ht[currentIndex] != -1) {
currentIndex = (currentIndex + 1) % m;
}
ht[currentIndex] = k;
count++;
}
void display() {
int i;
if (count == 0) {
printf("\nHash Table is empty");
return;
}
printf("\nHash Table contents are:\n ");
for (i = 0; i < m; i++)
printf("\n T[%d] --> %d ", i, ht[i]);
}
int main() {
int i;
printf("\nEnter the number of employee records (N): ");
scanf("%d", & n);
printf("\nEnter the two-digit memory locations (m) for the hash
table: ");
scanf("%d", & m);
ht = (int * ) malloc(m * sizeof(int));
for (i = 0; i < m; i++)
ht[i] = -1;
printf("\nEnter the four-digit key values (K) for N Employee
Records:\n");
for (i = 0; i < n; i++) {
int k;
scanf("%d", & k);
if (count == m) {
printf("\n~~~Hash table is full. Cannot insert the record %d
key~~~", i + 1);
break;
}
insert(k);
}
display();
return 0;
}

You might also like