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

Employee Dictionary Implementation

The document contains C++ code for an employee dictionary class that implements a hash table to store employee records. It includes methods for adding employees, displaying the hash table, and a main function for testing the functionality with sample employee data. The code includes placeholders for further implementation and debugging information.

Uploaded by

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

Employee Dictionary Implementation

The document contains C++ code for an employee dictionary class that implements a hash table to store employee records. It includes methods for adding employees, displaying the hash table, and a main function for testing the functionality with sample employee data. The code includes placeholders for further implementation and debugging information.

Uploaded by

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

#include <iostream>

#include <stdlib.h>
#include <string.h>
#include "sample.h"
using namespace std;

Emp_dictionary::Emp_dictionary()
{
// Update your code here
// The below code is added just to enable system test cases.
// You should delete it and insert your own code.
for(int i=0; i<10; i++)
hash_table[i] = 0;
}

Emp_dictionary::Emp::Emp(int num, char *nm)


{
// Update your code here
// You may want to compute hash_val here itself.
// The below code may be defective and is added just to enable system test
cases.
// You should delete it and insert your own code.
emp_no = num;
name = new char[strlen(nm)+1];
strcpy(name,nm);
next = NULL;
hash_val = num%9;
//add_emp(this);
}

void Emp_dictionary::add_emp(Emp *e)


{
// Update your code here
// The below code is added just to enable system test cases.
// You should delete it and insert your own code.
if(hash_table[e->hash_val]==0)
hash_table[e->hash_val]=e;
else
{
Emp* curr = hash_table[e->hash_val];
Emp* prev =NULL;
while(curr && (strcmp(e->name,curr->name)>0))
{
prev = curr;
curr = curr->next;
}
if(curr==NULL)
prev->next = e;
else if(curr==hash_table[e->hash_val])
{
e->next = curr;
hash_table[e->hash_val]=e;
}
else
{
e->next = curr;
prev->next = e;
}
}
}

void Emp_dictionary::display()
{
cout << endl;
for (int i=0; i<10; i++)
{
if(hash_table[i] == 0)
cout << "hash_table[" << i << "] : " << hash_table[i] << endl;
else
{
Emp *prev = NULL;
Emp *curr = hash_table[i];
cout << "hash_table[" << i << "] : ";
for (; curr != NULL; prev = curr, curr = curr->next)
cout << curr->name << " ";
cout << endl;
}
}
cout << endl;
}

#ifndef TESTING
int main( int arc, char **args)
{
Emp_dictionary Wipro;
Emp_dictionary::Emp Wipro_e1(1111, "abc");
Emp_dictionary::Emp Wipro_e2(22, "def"), Wipro_e3(123, "pqr"), Wipro_e4(42,
"aaa"), Wipro_e5(31, "bcd"), Wipro_e6(8446, "bbb");

cout << "\nWipro_e1: " << &Wipro_e1 << " " << Wipro_e1.emp_no << " " <<
Wipro_e1.hash_val << " " << Wipro_e1.name << endl;
cout << "Wipro_e2: " << &Wipro_e2 << " " << Wipro_e2.emp_no << " " <<
Wipro_e2.hash_val << " " << Wipro_e2.name << endl;
cout << "Wipro_e3: " << &Wipro_e3 << " " << Wipro_e3.emp_no << " " <<
Wipro_e3.hash_val << " " << Wipro_e3.name << endl;
cout << "Wipro_e4: " << &Wipro_e4 << " " << Wipro_e4.emp_no << " " <<
Wipro_e4.hash_val << " " << Wipro_e4.name << endl;
cout << "Wipro_e5: " << &Wipro_e4 << " " << Wipro_e5.emp_no << " " <<
Wipro_e5.hash_val << " " << Wipro_e5.name << endl;
cout << "Wipro_e6: " << &Wipro_e4 << " " << Wipro_e6.emp_no << " " <<
Wipro_e6.hash_val << " " << Wipro_e6.name << endl << endl;

Wipro.add_emp(&Wipro_e1);
Wipro.add_emp(&Wipro_e2); Wipro.add_emp(&Wipro_e3);
Wipro.add_emp(&Wipro_e4); Wipro.add_emp(&Wipro_e5);
Wipro.add_emp(&Wipro_e6);

cout << "Wipro dictionary:\n-------------------\n";


[Link]();

return 0;
}
#endif

Common questions

Powered by AI

Incorrect hash value calculations could lead to employees being misallocated within the hash table, causing entities to reside in incorrect or congested index chains. This results in inefficient lookups and potential data retrieval errors during operations attempting to fetch employee data based on expected hash positions .

The add_emp function handles collisions using a form of ordered linked list chaining. When a collision occurs, the function traverses the linked list at the collided index and inserts the new employee in a position sorted by name. If the colliding employee's name is lexicographically greater, it is appended at the end of the list .

The choice of 9 as a modulus in the hash function could adversely affect balance if employee numbers have patterns or common factors with 9, leading to non-uniform distribution and increased likelihood of collisions. Typically, using a prime number is preferred to enhance even distribution .

If dynamic memory allocation for names fails in the Emp constructor, it could lead to program termination due to unhandled exceptions or access to uninitialized pointers. This could be addressed by checking the return of memory allocation and implementing exception handling or fallback procedures to manage such failures .

The display function prints the hash table's contents by iterating over each index and then each element of any linked list present. Its efficiency might decrease with longer chains due to excessive traversal, especially in a densely populated table. An improvement could include optimizing the display to format output more efficiently or preprocessing links for faster traversal .

The display function, after executing the main, would show hash_table indices 0 to 9, each populated with names of employees that hash to those indices. For instance, employees with numbers hashing to 0 through 9 based on hash_val=num%9 will appear. Indices might feature multiple names if collisions occur, sorted lexicographically. Details depend on execution specifics and the order of names added to each hash chain .

The Emp class allocates memory for employee names using dynamic allocation with new and copies the name using strcpy. Potential issues include memory leaks if names are not properly deallocated, or dangling pointers if the class instance is destroyed improperly .

The Emp_dictionary class in the provided C++ code functions as a hash table for storing employee information. It supports adding new employee records using the add_emp method and displays the records using the display method .

The Emp class calculates hash values simply by taking the modulus of the employee number with 9 (hash_val = num%9). This approach is mathematically simplistic and could lead to poor distribution of entries across the hash table if employee numbers are not well dispersed, possibly increasing the chances of collisions .

The linked list structure plays a crucial role in collision handling by chaining colliding entries at the same hash index. However, weaknesses include increased time complexity for operations proportionate to longer chain lengths, which can degrade performance. Efficient collision handling mechanisms like balanced trees might offer better complexity in some scenarios .

You might also like