C Update Legacy Code Simple Hash Tables Accept Key String Map String Value First Name Last Q37240102

(C++)

Update legacy code for Simple Hash Tables so it willAccept a Key as String and Map to String Value.

First Name

Last Name

“Jane”

“Smith”

“John”

“Do”

“Susan”

“Collins”

“Bill”

“Rodgers”

“Eric”

“Jones”

“Bill”

“Wright”

“Mike”

“Bader”

Use Conventional Method Names Insert, Remove,Search:

insert(key, value)

insert(“First”, “Last”)

Search(“First”) → Method will return value

Remove(“First”) → Method will delete key andvalue

(Legacy Code):

#include<iostream>

#include<cstdlib>

#include<string>

#include<cstdio>

using namespace std;

const int TABLE_SIZE = 128;

/*

* HashEntry Class Declaration

*/

class HashEntry

{

  public:

      intkey;

      intvalue;

      HashEntry(int key,int value)

      {

          this->key= key;

          this->value= value;

      }

};

/*

* HashMap Class Declaration

*/

class HashMap

{

  private:

      HashEntry**table;

  public:

      HashMap()

{

          table= new HashEntry * [TABLE_SIZE];

          for(int i = 0; i< TABLE_SIZE; i++)

          {

              table[i]= NULL;

          }

      }

      /*

        * HashFunction

        */

      int HashFunc(intkey)

      {

          returnkey % TABLE_SIZE;

      }

      /*

        * InsertElement at a key

        */

void Insert(int key, int value)

{

          inthash = HashFunc(key);

          while(table[hash] != NULL && table[hash]->key !=key)

          {

              hash= HashFunc(hash + 1);

          }

          if(table[hash] != NULL)

              deletetable[hash];

          table[hash]= new HashEntry(key, value);

}

      /*

        * SearchElement at a key

        */

      int

OR
OR

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.