Hash dictionary fill code for insert, contains, remove, find. size, empty, load_
ID: 3818066 • Letter: H
Question
Hash dictionary fill code for insert, contains, remove, find. size, empty, load_factor, clear and bucket_count.
#ifndef __HASH_H
#define __HASH_H
#include <unordered_map>
#include <string>
#include <iostream>
using namespace std;
/*
private:
void rehash();
int hash_function(KEYTYPE key);
public:
bool insert(KEYTYPE key, VALTYPE val);
bool contains(KEYTYPE key);
int remove(KEYTYPE key);
VALTYPE* find(KEYTYPE key);
int size(); // Elements currently in table
bool empty(); // Is the hash empty?
float load_factor(); // Return current load factor
void clear(); // Empty out the table
int bucket_count(); // Total number of buckets in table
*/
template <typename KEYTYPE, typename VALTYPE>
class Hashtable
{
private:
// main hash table table
//vector< type > _hash;
/**
* Rehash the table into a larger table when the load factor is too large
*/
void rehash() {
}
/**
* Function that takes the key (a string or int) and returns the hash key
* This function needs to be implemented for several types it could be used with!
*/
int hash_function(int key) {
cout << " Hashing with int type keys." << endl;
}
int hash_function(string key) {
cout << " Hashing with string type keys." << endl;
}
public:
/**
* Basic constructor
*/
Hashtable( int startingSize = 101 )
{
}
/**
* Add an element to the hash table
*/
bool insert(KEYTYPE key, VALTYPE val) {
// Currently unimplemented
}
/**
* Return whether a given key is present in the hash table
*/
bool contains(KEYTYPE key) {
return false;
}
/**
* Completely remove key from hash table
* Returns number of elements removed
*/
int remove(KEYTYPE key) {
// Doesn't actually remove anything yet
}
/**
* Searches the hash and returns a pointer
* Pointer to Word if found, or nullptr if nothing matches
*/
VALTYPE* find(KEYTYPE key) {
return nullptr;
}
/**
* Return current number of elements in hash table
*/
int size() {
return(-1);
}
/**
* Return true if hash table is empty, false otherwise
*/
bool empty()
{
return(false);
}
/**
* Calculates the current load factor for the hash
*/
float load_factor() {
//return _hash.load_factor();
return (-1.0);
}
/**
* Returns current number of buckets (elements in vector)
*/
int bucket_count() {
return (-1);
}
/**
* Deletes all elements in the hash
*/
void clear() {
// Does nothing yet
}
};
#endif
Explanation / Answer
#define TABLESIZE 500
#define LENGTH 45
bool load(const char* lexicon)
{
/start hash table
node* hashtable[TABLESIZE];
/open lexicon and check
FILE* dict = fopen(dictionary, "r");
in the event that (dict == NULL)
{
printf("Could not open file.");
return false;
}
/start variable to store current word
char* dword = calloc(LENGTH+ 1,sizeof(char));
/read the document
while(fscanf(dict, "%s", dword) != EOF)
{
/if there is a word, make hub and place word in it
node* new_node = malloc(sizeof(node));
strcpy(new_node->word,dword);
/discover spot in hash table and place it in that container
unsigned int hashkey= 0;
for (int counter = 0; dword[counter]!= ''; counter++)
{
hashkey = (hashkey*dword[counter] + dword[counter] + counter)%TABLESIZE;
}
/check if spot in table exists; if not, begin the connected rundown
in the event that (hashtable[hashkey] == NULL)
{
hashtable[hashkey] = new_node;
new_node->next = NULL;
}
/generally made current hub the principal, move rest over
else
{
new_node->next = hashtable[hashkey];
hashtable[hashkey] = new_node;
}
/check words for later utilize
nwords++;
}
fclose(dict);
return genuine;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.