Question
Provide a main program. Your main program should do the following: (a) Create a hash table whose size is equal to the number of words
Provide a main program. Your main program should do the following:
(a) Create a hash table whose size is equal to the number of words in the dictionary.
(b) Read the words from the dictionary and store each one in the hash table.
(c) Output the statistics (i.e. minimum, maximum, and mean chain length) of the hash table after all words in the dictionary has been stored in the hash table.
(d) Read the words from the document and output each word that is NOT in the dictionary.
Example Test Run
as11_starter.cpp
#include
using namespace std;
int main() {
// a. Create a hash table whose size is equal to the number of words in the dictionary. fstream dictionary; dictionary.open("dictionary.txt", ios::in); if ( dictionary.fail() ) { cout
// b. Read the words from the dictionary and store each one in the hash table. HashTable
dictionary.close(); cout
// start checking if words are in dictionary.txt or not vector
dictionary.close(); cout
cout
SeperateChaining.h
#ifndef SEPARATE_CHAINING_H #define SEPARATE_CHAINING_H
#include #include
int nextPrime( int n );
// SeparateChaining Hash table class // // CONSTRUCTION: an approximate initial size or default of 101 // // ******************PUBLIC OPERATIONS********************* // bool insert( x ) --> Insert x // bool remove( x ) --> Remove x // bool contains( x ) --> Return true if x is present // int getCapacity( ) --> Return theLists.size() // int getCurrentSize( ) --> Return currentSize; // void makeEmpty( ) --> Remove all items
template
void makeEmpty( ) { for( auto & thisList : theLists ) thisList.clear( ); }
bool insert( const HashedObj & x ) { auto & whichList = theLists[ myhash( x ) ]; if( find( begin( whichList ), end( whichList ), x ) != end( whichList) ) return false; whichList.push_back( x );
// Rehash; see Section 5.5 if( ++currentSize > theLists.size( ) ) rehash( );
return true; } bool insert( HashedObj && x ) { auto & whichList = theLists[ myhash( x ) ]; if( find( begin( whichList ), end( whichList ), x ) != end( whichList ) ) return false; whichList.push_back( std::move( x ) );
// Rehash; see Section 5.5 if( ++currentSize > theLists.size( ) ) rehash( );
return true; }
bool remove( const HashedObj & x ) { auto & whichList = theLists[ myhash( x ) ]; auto itr = find( begin( whichList ), end( whichList ), x );
if( itr == end( whichList ) ) return false;
whichList.erase( itr ); --currentSize; return true; } void getStats( vector private: vector void rehash( ) { vector // Create new double-sized, empty table theLists.resize( nextPrime( 2 * theLists.size( ) ) ); for( auto & thisList : theLists ) thisList.clear( ); // Copy table over currentSize = 0; for( auto & thisList : oldLists ) for( auto & x : thisList ) insert( std::move( x ) ); // rvalue, no copy } size_t myhash( const HashedObj & x ) const { static hash /** * Internal method to test if a positive number is prime. * Not an efficient algorithm. */ bool isPrime( int n ) { if( n == 2 || n == 3 ) return true; if( n == 1 || n % 2 == 0 ) return false; for( int i = 3; i * i // To return a prime number at least as large as n, assumes n > 0. int nextPrime( int n ) { if( n % 2 == 0 ) ++n; for( ; !isPrime( n ); n += 2 ); return n; } // A hash routine for string objects. size_t hash( const string & key ) { size_t hashVal = 0; for( char ch : key ) hashVal = 37 * hashVal + ch; return hashVal; } // A hash routine for ints. size_t hash( int key ) { return key; } #endif> theLists; // The array of Lists int currentSize;
> oldLists = theLists;
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started