C Program Hash Table and Operations on it

How To Write a C Program to Creating hash table w.r.t employee age, display, delete and find average of salaries between the given age limits in C Programming Language ?


#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define SIZE 10
struct hash {
char name[20];
float salary;
int age;
struct hash* ptr;
};
een
struct hash *DICT[SIZE];
void makenull()
{
int i;
for(i=0; i<SIZE; i++)
{
DICT[i]=NULL; }
}

int hash(int age)
{
return(age%SIZE);
}
void insert()
{
char nam[20];
float sal;
int ag,i,index;
struct hash *oldptr;
printf("Enter the employee name\n");
scanf("%s",nam);
printf("Enter the salary\n");
scanf("%f", &sal);
printf("Enter the age\n");
scanf("%d",&ag);
index=hash(ag);
oldptr = DICT[index];
DICT[index]= (struct hash *) malloc(sizeof (struct hash));
// DICT[index]->name[20]=nam[20];
strcpy(DICT[index]->name, nam);
DICT[index]->salary=sal;
DICT[index]->age=ag;
DICT[index]->ptr=oldptr;
}
void display()
{
int index,age;
struct hash *head;
printf("Enter the employee age to display\n");
scanf("%d",&age);
index=hash(age);
head= DICT[index];
while (head != NULL)
{
printf("Name: %s\n",head->name);
printf("Age: %d\n", head->age);
printf("Salary: %f\n\n", head->salary);
head = head->ptr;
}
}

void delete()
{
int index,age;
char ename[20];
struct hash *head, *oldhead;
printf("Enter the employees' age to be deleted\n");
scanf("%d",&age);
index=hash(age);
head=DICT[index];
printf("Enter employee name to be deleted\n");
scanf("%s",ename);
while ( head != NULL )
{
if (! strcmp (head->name, ename) )
{
oldhead=head->ptr;
DICT[index]=oldhead;
free(head);
}
head=head->ptr;
}
}
void avg()
{
int age1,age2, index1;
struct hash *head;
float asal;
printf("Calculating average of salaries\n");
printf("Enter range of employee age\n");
printf("From (Enter starting age)\n");
scanf("%d", &age1);
printf("To (Enter ending age)\n");
scanf("%d", &age2);
while (age1 <= age2)
{
index1=hash(age1);
head=DICT[index1];
while( head !=NULL)
{
asal=asal+head->salary;
head=head->ptr;
}
age1++;
}
printf("The average of salary is: %f", asal);
}
int main ()
{
int num,i,choice;
printf("Enter the number of employee details you want to enter\n");
scanf("%d",&num);
makenull();
for(i=0; i<num; i++)
{
insert();
}
printf("What do you want to do now\n");
do {
printf("Enter your choice\n");
printf("1->Display\n2->Search and Delete\n3->Find average salary\n0->Exit\n");
scanf("%d",&choice);
switch(choice)
{
case 1: display();
break;
case 2: delete();
break;
case 3: avg();
break;
case 0: break;
}
} while ( choice != 0);
return 0;
}
 Tags: Hash table and operations on it, hash table in operating system, hash tables java, hash tables data structures, hash tables python, hash tables tutorial, hash tables powershell, hash tables ppt, hash tables in sql server.


Learn More :