This is my first time working with hash tables so bare with me if im waaaaay off lol. This is the assignment
. Here we will use the method known as chaining, in which the hash table is implemented using a vector (or array) of linked lists.
When an item is to be inserted into the table, a hashing function h is applied to the item to determine where it is to be placed in the table; for example, a common one is
h(item) = item % table-size
where the table-size is usually taken to be a prime number (to scatter ("hash") the items throughout the table. For non-numeric items, numeric codes e,g., ASCII are used. The linked list at location h(item) is searched for the item. If it is not found, the item is inserted into the linked list at this location.
Design a HashTable class for processing hash tables. Use a small prime number such as 11 or 13 for the table size. Two of the basic operations it must provide are:
1. Insert an item into the hash table as just described. It doesn't matter where in the linked list it is inserted.
2. Display each index in the hash table and a list of all the items stored at that location.
For this particular problem, the hash table is to store strings and use the following hash function:
h(str) = (sum of the ASCII codes of the first 3 characters of str) % table_size.
For strings with fewer than 3 characters, just use whatever characters there are in the string.
Here is the header file I wrote. I know there will probably be a few things wrong but im new to this so let me know what I'm doing wrong here. Please and thank you.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
|
#include <iostream>
#ifndef HASHTABLE
#define HASHTABLE
using namespace std;
typedef string HashElement;
class HashTable
{
public:
HashTable() //default constructor, written inline
{
char numElement;
}
HashTable(const HashTable & original); //copy constructor
~HashTable(); //destructor
void Insert(const HashElement & item);
void Display (ostream & out) const;
private:
int tableSize;
HashElement *myVector;
HashElement Item; //string item;?
};
#endif
|