If Screen is half, please rotate the Mobile then it is full program available
using linear probing.
#include <stdio.h>
#include <conio.h>
#include <stdlib.h>
#include <string.h>
#define NO_OF_BUCKETS 13
typedef struct EmployeeRecord
{
char SSN[20];
char name[20];
char dept[20];
char salary[20];
}Record;
typedef struct Node *ListPointer;
typedef struct Node
{
Record data;
ListPointer link;
}Node;
typedef enum
{
available, duplicate, bucket_full
}Status;
ListPointer ht[NO_OF_BUCKETS];
int homeBucket, currentBucket;
Record getNextRecord ( void )
{
Record data;
scanf ( "%s", data.SSN );
scanf ( "%s", data.name );
scanf ( "%s", data.dept );
scanf ( "%s", data.salary );
return data;
}
ListPointer getNode ( Record data )
{
ListPointer temp;
temp = ( ListPointer ) malloc ( sizeof ( *temp ) );
if ( temp == NULL )
{
printf ( "\n\n Allocation Failed\n\n" );
getchar (); //getch();
exit ( EXIT_FAILURE );
}
else
{
temp->data = data;
}
return temp;
}
int stringToInt ( char *key )
{
int number = 0;
while ( *key )
number += *key ++;
return number;
}
int myHash ( char *key )
{
int k;
k = stringToInt ( key );
return k % NO_OF_BUCKETS;
}
Status search ( char *key )
{
homeBucket = myHash ( key );
currentBucket = homeBucket;
while ( ht[currentBucket]
&&
strcmp ( ht[currentBucket]->data.SSN, key ) != 0 )
{
currentBucket = ( currentBucket + 1 ) % NO_OF_BUCKETS;
/* treat the table as circular */
if ( currentBucket == homeBucket )
return bucket_full; /* back to start point */
}
if ( ht[currentBucket] )
{
if ( strcmp ( ht[currentBucket]->data.SSN, key ) == 0 )
return duplicate;
}
return available;
}
void insert ( Record data )
{
Status state;
state = search ( data.SSN);
switch ( state )
{
case available: if ( ht[homeBucket] != NULL )
{
printf ( "\n\n Collision is Detected at the Index:
%d", homeBucket );
printf ( "\n\n Issue is solved by Linear probing
with the new Index: %d\n\n", currentBucket );
}
ht[currentBucket] = getNode ( data );
break;
case duplicate: printf("\nDuplicate key: %s\n", data.SSN);
break;
case bucket_full: printf("\nAll buckets are occupied\n");
}
}
void display ( void )
{
int i;
printf("\nEmployee details...\n");
printf("\nINDEX\tSSN\tNAME\tDEPT.\tSALARY\n");
for ( i = 0; i < NO_OF_BUCKETS; ++ i )
{
if ( ht[i])
{
printf ( "\n[%d] - %s\t", i, ht[i]->data.SSN );
printf ( "%s\t", ht[i]->data.name );
printf ( "%s\t", ht[i]->data.dept );
printf ( "%s\n", ht[i]->data.salary );
}
else
{
printf ( "\n[%d] - %s\n", i, " " );
}
}
}
int main ( void )
{
Record data;
int nRecords;
int choice;
int i;
printf("\n..HASH DEMONSTRATION WITH LINEAR PROBING..\n");
printf ( "\n\n1. Insertion\n\n2. Display\n\n3. Exit\n");
while ( 1 )
{
printf ( "\nEnter your Choice: " );
scanf ( "%d", &choice );
switch ( choice )
{
case 1: printf ( "\nHow many records: " );
scanf ( "%d", &nRecords );
printf("\nGive %d record details one by one\n", nRecords);
printf("\nSSN\tNAME\tDEPT.\tSALARY\n");
for ( i = 0; i < nRecords; ++ i )
{
data = getNextRecord();
insert ( data );
}
break;
case 2: display ( );
break;
case 3: return 0;
default: printf ( "\nWrong Choice\n" );
}
}
}
output:
..HASH DEMONSTRATION WITH LINEAR PROBING..
1. Insertion
2. Display
3. Exit
Enter your Choice: 1
How many records: 6
Give 6 record details one by one
SSN NAME DEPT. SALARY
One Martin CSE 1111
Two Kate CSE 2222
Three Brian ISE 5555
Four Kathy ECE 4444
Five Daniel ECE 3333
Collision is Detected at the Index: 10
Issue is solved by Linear probing with the new Index: 11
six Emily ISE 8888
Collision is Detected at the Index: 2
Issue is solved by Linear probing with the new Index: 4
Enter your Choice: 2
Employee details...
iNDEX SSN NAME DEPT. SALARY
[0] -
[1] -
[2] - four Kathy ECE 4444
[3] - three Brian ISE 5555
[4] - six Emily ISE 8888
[5] -
[6] -
[7] -
[8] - two Kate CSE 2222
[9] -
[10] - one Martin CSE 1111
[11] - five Daniel ECE 3333
[12] -
Enter your Choice: 3
Exit
..HASH DEMONSTRATION WITH LINEAR PROBING..
1. Insertion
2. Display
3. Exit
Enter your Choice: 1
How many records: 3
Give 3 record details one by one
SSN NAME DEPT. SALARY
one John CSE 1111
two Kathy ISE 2222
one John CSE 1111
Duplicate key: one
Enter your Choice: 3
Exit
..HASH DEMONSTRATION WITH LINEAR PROBING..
1. Insertion
2. Display
3. Exit
Enter your Choice: 1
How many records: 14
Give 14 record details one by one
SSN NAME DEPT. SALARY
one John CSE 1111
two Kathy CSE 2222
three Martin ISE 3333
four Emily Ise 4444
five Helena ECE 5555
Collision is Detected at the Index: 10
Issue is solved by Linear probing with the new Index: 11
six Joan ECE 6666
Collision is Detected at the Index: 2
Issue is solved by Linear probing with the new Index: 4
seven Daniel CSE 2222
eight Brook ISE 5555
nine Boby ISE 6666
Collision is Detected at the Index: 10
Issue is solved by Linear probing with the new Index: 0
ten Jack CSE 7777
Collision is Detected at the Index: 2
Issue is solved by Linear probing with the new Index: 5
eleven Tim ISE 8888
Collision is Detected at the Index: 2
Issue is solved by Linear probing with the new Index: 6
twelve Roger CSE 9999
Collision is Detected at the Index: 0
Issue is solved by Linear probing with the new Index: 1
thirty Sarah ISE 1111
Collision is Detected at the Index: 0
Issue is solved by Linear probing with the new Index: 7
fourty Curry ECE 3333
All buckets are occupied
Enter your Choice: 2
Employee details...
iNDEX SSN NAME DEPT. SALARY
[0] - nine Boby ISE 6666
[1] - twelve Roger CSE 9999
[2] - four Emily Ise 4444
[3] - three Martin ISE 3333
[4] - six Joan ECE 6666
[5] - ten Jack CSE 7777
[6] - eleven Tim ISE 8888
[7] - thirty Sarah ISE 1111
[8] - two Kathy CSE 2222
[9] - eight Brook ISE 5555
[10] - one John CSE 1111
[11] - five Helena ECE 5555
[12] - seven Daniel CSE 2222
Enter your Choice: 3
Exit