| | |
Hash Table Implementation
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
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.
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.
C++ Syntax (Toggle Plain Text)
#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
Last edited by Narue; Nov 9th, 2008 at 10:18 am. Reason: snipped email
~Albert
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]
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]
•
•
•
•
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]
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.
~Albert
![]() |
Similar Threads
- Dictionary ADT (Computer Science)
- Assmbler implementation using C (C)
- Simple Hash Function for Integer Arrays (C)
- Hash Table template implementation help (C++)
Other Threads in the C++ Forum
- Previous Thread: Encoder
- Next Thread: Where does Modulus fit in with PEMDAS?
| Thread Tools | Search this Thread |
api array based beginner binary c++ c/c++ calculator char char* class classes code compile compiler console conversion count delete deploy desktop directshow dll download dynamic dynamiccharacterarray email encryption error file forms fstream function functions game givemetehcodez google graph gui homeworkhelp homeworkhelper iamthwee ifstream input int integer java lib linkedlist linker linux list loop looping loops map math matrix memory news node numbertoword output parameter pointer problem program programming project python random read recursion recursive reference return rpg sorting string strings struct temperature template templates test text text-file tree unix url variable vector video visualstudio win32 windows winsock word wordfrequency wxwidgets






