DaniWeb IT Discussion Community

DaniWeb IT Discussion Community (http://www.daniweb.com/forums/index.php)
-   C++ (http://www.daniweb.com/forums/forum8.html)
-   -   Hash Table Implementation (http://www.daniweb.com/forums/thread156175.html)

a.self Nov 9th, 2008 4:31 am
Hash Table Implementation
 
1 Attachment(s)
Hi everyone,
Since it's the weekend, I can't get assistance from my professor or his assistant until Monday.
My deadline for submission is approaching and I'd feel a lot safer getting as much work as I can.

I'm implementing a hash table to store strings using linear probing. My implementation results in my program hanging and I would like some insight as to why it's hanging. Any help would be appreciated.

I'm sure the problem lies within my linear probing function and/or the comparison of strings.
Omitting ! produces a result but the desired one.

If you have further questions pm or email at [email removed].

Preview of the HashDef.h
I've included all the files necessary to test any modification you might make.

Thank you in advance for any help as well as for your time.

#ifndef H_HashDef
#define H_HashDef

#include <iostream>
#include <string>
#include <fstream>
#include <cctype>
#include <iomanip>
using namespace std;



class HtType
{
public:
        HtType();
        //default constructor
        //initializes Size, the number
        //of current elements in the table
        void ReadData();
        int hashFunction(const char *key, int keyLenght);
        void LinearProbing();
        void PrintData();
               
private:
        int size;
        string word[131];
        const char *word2;
        int len;
        int hIndex;
        int HTSIZE;
        string hashTable[131];
        int IndexStatusList[131];
       
};
HtType::HtType()
{
        size = 0;
        HTSIZE = 131;
        for(int b = 0; b < HTSIZE;b++)
        {
                IndexStatusList[b] = 0;
        }
        for(int c = 0; c < HTSIZE; c++)
        {
                hashTable[c] = " ";
        }
}
void HtType::ReadData()
{
        ifstream DataIn;

        DataIn.open("words.txt");
        if(!DataIn)
        {
                cout <<"Error opening data file\n";
        }
        else
        {
                DataIn >> word[size];
                size++;
                while(!DataIn.eof())
                {
                DataIn >> word[size];
                size++;
                }

               
        }
}
int HtType::hashFunction(const char *key, int keyLenght)
{
        int sum = 0;
        for(int j =0; j < keyLenght ; j++)
        {
                sum = sum + static_cast<int>(tolower(key[j]));
               
        }
        return sum = sum % HTSIZE;
}
void HtType::LinearProbing()
{
        for(int a = 0; a < size; a++)
        {
                len = static_cast<int>(word[a].length());
                word2 = word[a].data();              //copies string as a c-string argument
                hIndex = hashFunction(word2,len);
                //int i = 1;
                if(IndexStatusList[hIndex] == 0)
                {
                        hashTable[hIndex] = word[a];
                        IndexStatusList[hIndex] = 1;
                }
                else
                {
                        int i = 1;
                while(IndexStatusList[hIndex] != 0)
                {
                        if(!word[a].compare(word2))
                        {
                                int addone = IndexStatusList[hIndex];
                                addone++;
                                IndexStatusList[hIndex] = addone;
                        }
                        else
                        {
                        hIndex = (hashFunction(word2,len) + i) % HTSIZE;
                        i++;
                        }
                }
                        hashTable[hIndex] = word[a];
                        IndexStatusList[hIndex] = 1;
                }
               
        }
}
void HtType::PrintData()
{
        for(int a = 0; a < HTSIZE; a++)
        {
                cout << hashTable[a]<< "\n";       
        }
       

}
#endif

ArkM Nov 9th, 2008 7:01 am
Re: Hash Table Implementation
 
1. It seems you don't implement any hash tables. A hash table is a container with insert, find and (may be) erase operations. But I can see only chimerical class - a mix of a fixed data file reading from unknown (for me) source, mysterious linearProbing method and final printing. Do you know that there is a wonderful C++ lexical construct called comment?..
2. Why the attachment is so big (1M) zip? All sources of your project must have a packed size less than 2-3K...
3. Next time use code tag with a language specifier:
[code=cplusplus]
... C++ source codes ...
[/code]

a.self Nov 9th, 2008 1:21 pm
Re: Hash Table Implementation
 
Quote:

Originally Posted by ArkM (Post 731801)
1. It seems you don't implement any hash tables. A hash table is a container with insert, find and (may be) erase operations. But I can see only chimerical class - a mix of a fixed data file reading from unknown (for me) source, mysterious linearProbing method and final printing. Do you know that there is a wonderful C++ lexical construct called comment?..
2. Why the attachment is so big (1M) zip? All sources of your project must have a packed size less than 2-3K...
3. Next time use code tag with a language specifier:
[code=cplusplus]
... C++ source codes ...
[/code]

Thanks for the advice. I didn't understand the syntax highlighting.
My program doesn't require me to use any of those functions yet.
I couldn't if I wanted to at this moment.
The linear probing should just look for the next available and empty slot in the array when collision occurs.
My hashfunction assigns a value to hIndex by calculating the ASCII value of the string.

I'm currently trying a different approach i thought of this morning.
Thanks again for your time.


All times are GMT -4. The time now is 10:27 pm.

Forum system based on vBulletin Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
©2003 - 2009 DaniWeb® LLC