0% found this document useful (0 votes)
8 views2 pages

Program 12 DSLab

This is lab program I want to prefer notes , To read easy

Uploaded by

ishasingh4319
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views2 pages

Program 12 DSLab

This is lab program I want to prefer notes , To read easy

Uploaded by

ishasingh4319
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

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>
#define MAX_ADDR 5

struct employee
{
int id, age;
char name[25];
} emp[MAX_ADDR];

int hash(int key)


{
return key % MAX_ADDR;
}

int main(void)
{
int i, ch, count = 0, index, haddr, id;
for (;;)
{
printf("Enter 1 to insert record\n2 to search record\n");
scanf("%d", &ch);
switch (ch)
{
case 1:
if (count == MAX_ADDR)
{
printf("No free address space\n");
break;
}
printf("Enter employee id: ");
scanf("%d", &id);
haddr = hash(id);
printf("Home address is %d\n", haddr);
for (i = 0; i < MAX_ADDR; i++)
{
index = (haddr + i) % MAX_ADDR;
if (emp[index].id == 0)
emp[index].id = id;
printf("Enter the employee name: ");
scanf("%s", emp[index].name);
printf("Enter the employee age: ");
scanf("%d", &emp[index].age);
count++;
printf("Successfully inserted at Actual Address %d:\n", index);
break;
}
}
break;
case 2:
printf("Enter employee id to be searched: ");
scanf("%d", &id);
haddr = hash(id);
for (i = 0; i < MAX_ADDR; i++)
{
index = (haddr + i) % MAX_ADDR;
if (emp[index].id == 0)
{
printf("Key not present\n");
break;
} else if (emp[index].id == id)
{
printf("Employee id: %d\n", emp[index].id);
printf("Employee name: %s\n", emp[index].name);
printf("Employee age: %d\n", emp[index].age);
printf("Search Length is: %d\n", i + 1);
break;
}
}
break;
default:
return 0;
}
}
}

You might also like