Question

Create a hash table in C++ for names (first name and last name only, no telephone...

Create a hash table in C++ for names (first name and last name only, no telephone numbers), and use the following to generate the keys:

unsigned char key = 0;

for (int i = 0; i < fullName.length(); i++)

        key ^= fullName[i];

Your instructor will be using the following list of names to test your program:

Uzoma Acholonu

Giuliana Asmad

Michael Atkins-Combs

Vishnu Bakthisaran

Christopher Blowars

William Bronson

Trevor Butcher

Tiffany Caceres-Bonilla

Dulce Castro

David Cifuentes

Daniel Coursen

Alexandra Davilla

Amanda Dewitt

Alfredo Diaz

Jan Espinosa

Anthony Farrisi

Seth Fenstermaker

Daniella Gabout

Regina Green

Kelly Gregory

Bryan Houser

Michael Jackamorris

Kevin Kim

Benjamin Lee

Kyle Lighting

Perlamassiel Lopez

Omran Losinno

Lisa Maine

Derrike Mason

Andrew McCalla

Alexander McConnell

Niral Modi

Aida Montanez

Christopher Price

Daniyal Raza

Morgan Rex

Andres Rivero

Thomas Rudnicki

Kyle Russell

Samantha Rutkowski

Jaskamal Saini

Kyle Schmidt

Taylor Schnappauf

Ryan Snyder

Heazel Souid

Jason Spiegel

Minh Tang

Tehillah Trauger

Alexis Tocci

Luis Virola

Dale Wenger

Malik White

Jaimie Williams

When done loading the hash table print out the contents of each occupied entry, and any other entries pointed to by an occupied entry in the following format (use tabs, the columns do not have to line up):

Slot 51 is occupied with

Alexandra Davilla Ryan Snyder        

Slot 56 is occupied with

William Bronson            Derrike Mason   Taylor Schnappauf        Jason Spiegel

You do NOT have to print out the statistics

Homework Answers

Answer #1

#include<iostream>
#include<cstdlib>
#include<cstring>
using namespace std;

//Defining structure of list fo

struct lnode{
   char name[30];
   struct lnode *next;
};

//defining structure for the table.

struct node{
   int key;
   struct node *nkey;
   struct lnode *list;
}*start;

//Class Declaration

class table{
   public:
   node* create_node(int);
   lnode* create_lnode(node*);
   void insert_name();
   void display();
   table(){
       start = NULL;
   }
};

int main(){
   int choice;
   table t;
   start = NULL;
   cout<<endl<<"";
   cout<<endl<<"*****Creating Hash Table*****"<<endl;
   while(1){
       cout<<endl<<"Options:"<<endl;
       cout<<endl<<"1. New Entry.";
       cout<<endl<<"2. Display.";
       cout<<endl<<"3. Exit.";
       cout<<endl<<"Enter your choice: ";
       cin>>choice;
       switch(choice){
           case 1:
               t.insert_name();
               cout<<endl;
               break;
           case 2:
               t.display();
               cout<<endl;
               break;
           case 3:
               cout<<"Exiting..."<<endl;
               exit(1);
               break;
           default:
               cout<<endl<<"Wrong choice."<<endl;
       }
   }
   return 0;
}

//Searching key in the list.

node* table::create_node(int k){
   struct node *temp, *p;
   temp->nkey = NULL;
   temp->list = NULL;
   p = start;
   while(p->nkey != NULL){
       if(p->key == k)
           return p;  
       p = p->nkey;
   }
   if(start == NULL){
       start = temp;
   }
   else{
       p->nkey = temp;      
   }
   return temp;
}

//Allocating space for new name.

lnode* table::create_lnode(struct node *t){
   struct lnode *temp, *p;
   strcpy(temp->name, "");
   temp->next = NULL;
   if(t->list == NULL)
       t->list = temp;
   else{
       p = t->list;
       while(p->next != NULL)
           p = p->next;
       p->next = temp;
   }
   return temp;
}

//Defining function to insert name.

void table::insert_name(){
   char fullname[30];
   int i, key;
   struct node *temp;
   struct lnode *location;
   cout<<endl<<"Enter the name: ";
   cin>>fullname;
   /*
   *Calculating key
   */
   for(i=0;i<strlen(fullname);i++)
       key ^= fullname[i];
   /*
   *Allocating space for the key
   */
   temp = create_node(key);
   /*
   *Allocating space for new name.
   */
   location = create_lnode(temp);
   //Storing the name in correct position.
   strcpy(location->name, fullname);
}

//Defining function to display the table.

void table::display(){
   struct node *p;
   struct lnode *q;
   p = start;
   cout<<endl<<"The hash table is:"<<endl;
   while(p != NULL){
       cout<<"Key: "<<p->key;
       q = p->list;
       while(q != NULL){
           cout<<"   "<<q->name;
           q = q->next;
       }
       p = p->nkey;
       cout<<endl;
   }
}

Know the answer?
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for?
Ask your own homework help question
Similar Questions
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT