Jump to content

Hash tables

- - - - -

  • Please log in to reply
2 replies to this topic

#1
camouser1s

camouser1s

    Newbie

  • Members
  • Pip
  • 6 posts
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.


#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 




#2
Flying Dutchman

Flying Dutchman

    Programming God

  • Members
  • PipPipPipPipPipPipPip
  • 889 posts
  • Location:::1
Why use typedef at all? Seems like it confused you since Item property is std::string, but myVector is pointer to std::string and you probably want std::vector. Headers look ok, you might need to include <string> though.
A conclusion is where you got tired of thinking.
#define class struct    // All is public.

#3
dargueta

dargueta

    Writes binary right handed and hex left handed

  • Moderators
  • 4,718 posts
  • Programming Language:C, Java, C++, PHP, Python, Perl, Assembly, Bash, Others
  • Learning:JavaScript
A few things:
1) Try not to use using namespace X. It could cause name collisions (i.e. different namespaces with types of the same name) that could take forever to catch and fix, especially in larger programs. It's better to write std::string. For something like this, I guess you're fine, but keep this in the back of your mind when you're coding larger projects.

2) I don't see a reason for your char numElement declaration, as it goes out of scope immediately and is never used.

3) FlyingDutchman is right; you do need to include <string>, but not necessarily <iostream>. If you do decide to use std::vector like FD suggested, be sure to include <vector> or the compiler will barf.

Good work so far!
sudo rm -rf /




1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users