Hash Table Implementation

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Nov 2008
Posts: 4
Reputation: a.self is an unknown quantity at this point 
Solved Threads: 0
a.self's Avatar
a.self a.self is offline Offline
Newbie Poster

Hash Table Implementation

 
0
  #1
Nov 9th, 2008
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.

  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
Last edited by Narue; Nov 9th, 2008 at 10:18 am. Reason: snipped email
Attached Files
File Type: zip Copy of Assignment3.zip (1.01 MB, 10 views)
~Albert
Reply With Quote Quick reply to this message  
Join Date: Jul 2008
Posts: 2,001
Reputation: ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of 
Solved Threads: 343
ArkM's Avatar
ArkM ArkM is offline Offline
Postaholic

Re: Hash Table Implementation

 
0
  #2
Nov 9th, 2008
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]
Reply With Quote Quick reply to this message  
Join Date: Nov 2008
Posts: 4
Reputation: a.self is an unknown quantity at this point 
Solved Threads: 0
a.self's Avatar
a.self a.self is offline Offline
Newbie Poster

Re: Hash Table Implementation

 
0
  #3
Nov 9th, 2008
Originally Posted by ArkM View Post
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.
~Albert
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC