943,545 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 9794
  • C++ RSS
Nov 9th, 2008
0

Hash Table Implementation

Expand Post »
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.

C++ Syntax (Toggle Plain Text)
  1. #ifndef H_HashDef
  2. #define H_HashDef
  3.  
  4. #include <iostream>
  5. #include <string>
  6. #include <fstream>
  7. #include <cctype>
  8. #include <iomanip>
  9. using namespace std;
  10.  
  11.  
  12.  
  13. class HtType
  14. {
  15. public:
  16. HtType();
  17. //default constructor
  18. //initializes Size, the number
  19. //of current elements in the table
  20. void ReadData();
  21. int hashFunction(const char *key, int keyLenght);
  22. void LinearProbing();
  23. void PrintData();
  24.  
  25. private:
  26. int size;
  27. string word[131];
  28. const char *word2;
  29. int len;
  30. int hIndex;
  31. int HTSIZE;
  32. string hashTable[131];
  33. int IndexStatusList[131];
  34.  
  35. };
  36. HtType::HtType()
  37. {
  38. size = 0;
  39. HTSIZE = 131;
  40. for(int b = 0; b < HTSIZE;b++)
  41. {
  42. IndexStatusList[b] = 0;
  43. }
  44. for(int c = 0; c < HTSIZE; c++)
  45. {
  46. hashTable[c] = " ";
  47. }
  48. }
  49. void HtType::ReadData()
  50. {
  51. ifstream DataIn;
  52.  
  53. DataIn.open("words.txt");
  54. if(!DataIn)
  55. {
  56. cout <<"Error opening data file\n";
  57. }
  58. else
  59. {
  60. DataIn >> word[size];
  61. size++;
  62. while(!DataIn.eof())
  63. {
  64. DataIn >> word[size];
  65. size++;
  66. }
  67.  
  68.  
  69. }
  70. }
  71. int HtType::hashFunction(const char *key, int keyLenght)
  72. {
  73. int sum = 0;
  74. for(int j =0; j < keyLenght ; j++)
  75. {
  76. sum = sum + static_cast<int>(tolower(key[j]));
  77.  
  78. }
  79. return sum = sum % HTSIZE;
  80. }
  81. void HtType::LinearProbing()
  82. {
  83. for(int a = 0; a < size; a++)
  84. {
  85. len = static_cast<int>(word[a].length());
  86. word2 = word[a].data(); //copies string as a c-string argument
  87. hIndex = hashFunction(word2,len);
  88. //int i = 1;
  89. if(IndexStatusList[hIndex] == 0)
  90. {
  91. hashTable[hIndex] = word[a];
  92. IndexStatusList[hIndex] = 1;
  93. }
  94. else
  95. {
  96. int i = 1;
  97. while(IndexStatusList[hIndex] != 0)
  98. {
  99. if(!word[a].compare(word2))
  100. {
  101. int addone = IndexStatusList[hIndex];
  102. addone++;
  103. IndexStatusList[hIndex] = addone;
  104. }
  105. else
  106. {
  107. hIndex = (hashFunction(word2,len) + i) % HTSIZE;
  108. i++;
  109. }
  110. }
  111. hashTable[hIndex] = word[a];
  112. IndexStatusList[hIndex] = 1;
  113. }
  114.  
  115. }
  116. }
  117. void HtType::PrintData()
  118. {
  119. for(int a = 0; a < HTSIZE; a++)
  120. {
  121. cout << hashTable[a]<< "\n";
  122. }
  123.  
  124.  
  125. }
  126. #endif
Attached Files
File Type: zip Copy of Assignment3.zip (1.01 MB, 347 views)
Last edited by Narue; Nov 9th, 2008 at 10:18 am. Reason: snipped email
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
a.self is offline Offline
4 posts
since Nov 2008
Nov 9th, 2008
0

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]
Reputation Points: 1234
Solved Threads: 347
Postaholic
ArkM is offline Offline
2,001 posts
since Jul 2008
Nov 9th, 2008
0

Re: Hash Table Implementation

Click to Expand / Collapse  Quote originally posted by ArkM ...
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.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
a.self is offline Offline
4 posts
since Nov 2008

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: Encoder
Next Thread in C++ Forum Timeline: Where does Modulus fit in with PEMDAS?





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC