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

Hash Program

The document contains a C program that implements a hash table for storing employee records. It allows the user to input the number of records and the size of the hash table, then inserts keys using linear probing for collision resolution. Finally, it displays the contents of the hash table or indicates if it is full.

Uploaded by

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

Hash Program

The document contains a C program that implements a hash table for storing employee records. It allows the user to input the number of records and the size of the hash table, then inserts keys using linear probing for collision resolution. Finally, it displays the contents of the hash table or indicates if it is full.

Uploaded by

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

Program 8- Hash

#include<stdio.h>
#include<stdlib.h>
int key[20],n,m;
int *ht,index;
int count = 0;
void insert(int key)
{
index = key % m;
while(ht[index] != -1)
{
index = (index+1)%m;
}
ht[index] = key;
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]);
}
void main()
{
int i;
printf("\nEnter the number of employee records (N) : ");
scanf("%d", &n);
printf("\nEnter the two digit memory locations (m) for 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++)
scanf("%d", &key[i]);
for(i=0;i<n;i++)
{
if(count == m)
{
printf("\n~~~Hash table is full. Cannot insert the record %d
key~~~",i+1);
break;
}
insert(key[i]);
}
//Displaying Keys inserted into hash table
display();
}

You might also like