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

Code Express

The document discusses code for a hash table implementation of an employee dictionary. It defines classes for employees and the dictionary, and includes methods to add employees to the hash table and display the contents.

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)
18 views

Code Express

The document discusses code for a hash table implementation of an employee dictionary. It defines classes for employees and the dictionary, and includes methods to add employees to the hash table and display the contents.

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
You are on page 1/ 2

#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";


Wipro.display();

return 0;
}
#endif

You might also like