Project help

Reply

Join Date: Feb 2007
Posts: 10
Reputation: zakula11 is an unknown quantity at this point 
Solved Threads: 0
zakula11 zakula11 is offline Offline
Newbie Poster

Project help

 
0
  #1
Feb 14th, 2007
Hey, for part of my project, I have to implement a Hash Table template. I had to implement several other classes too, and I finished them. But I really don't know much at all about hash tables and templates, and am not sure how to go about this at all... If someone can give me some help or direction that would be great.

The code I am given is below... I have to fill in the TODO's.
Thanks.


  1.  
  2.  
  3. //
  4. // CS251 Data Structures
  5. // Hash Table template
  6. //
  7. #include <assert.h>
  8. #include <stdlib.h>
  9. #include <string.h>
  10. ///////////////////////////////////////////
  11. //
  12. // Class that defines a template for a hash table
  13. // The keys are of type const char * and the data
  14. // can be any type.
  15. //
  16. // Use like this:
  17. // HashTable hashTableInt<int>;
  18. //
  19. //
  20. ///////////////////////////////////////////
  21. // Each hash entry stores a key, object pair
  22. template <typename Data>
  23. struct HashTableEntry {
  24. const char * _key;
  25. Data _data;
  26. HashTableEntry * _next;
  27. };
  28.  
  29. // This is a Hash table that maps string keys to objects of type Data
  30. template <typename Data>
  31. class HashTable {
  32. public:
  33. // Number of buckets
  34. enum { TableSize = 2039};
  35. // Array of the hash buckets.
  36. HashTableEntry<Data> **_buckets;
  37.  
  38. // Obtain the hash code of a key
  39. int hash(const char * key);
  40. public:
  41. HashTable();
  42.  
  43. // Add a record to the hash table. Returns true if key already exists.
  44. // Substitute content if key already exists.
  45. bool insertItem( const char * key, Data data);
  46.  
  47. // Find a key in the dictionary and place in "data" the corresponding \
  48. record
  49. // Returns false if key is does not exist
  50. bool find( const char * key, Data * data);
  51.  
  52. // Removes an element in the hash table. Return false if key does not
  53. //exist.
  54. bool removeElement(const char * key);
  55. };
  56.  
  57. template <typename Data>
  58. int HashTable<Data>::hash(const char * key)
  59. {
  60. // TODO: Compute the hash number from the key string
  61. return (int)key;
  62. }
  63.  
  64. template <typename Data>
  65. HashTable<Data>::HashTable()
  66. {
  67. // TODO: Initialize the hash table
  68. }
  69.  
  70. template <typename Data>
  71. bool HashTable<Data>::insertItem( const char * key, Data data)
  72. {
  73. // TODO: Insert a key,data pair inside the hash table.
  74. // Return true if the entry already exists or false if
  75. // it does not exist.
  76. return false;
  77. }
  78.  
  79. template <typename Data>
  80. bool HashTable<Data>::find( const char * key, Data * data)
  81. {
  82. // TODO: Find the data associated to a key. The data is
  83. // stored in the *data passed as parameter.
  84. // Return true if the entry is found or false otherwise.
  85. return false;
  86. }
  87.  
  88. template <typename Data>
  89. bool HashTable<Data>::removeElement(const char * key)
  90. {
  91. // TODO: Remove the element that has this key from the hash table.
  92. // Return true if the entry is found or false otherwise.
  93. return false;
  94. }
  95. /////////////////////////////////////
  96. //
  97. // Class used to iterate over the hash table
  98. //
  99. /////////////////////////////////////
  100.  
  101. template <typename Data>
  102. class HashTableIterator {
  103. int _currentBucket; // Current bucket that is being iterated
  104. HashTableEntry<Data> *_currentEntry; // Current entry iterated
  105. HashTable<Data> * _hashTable; // Pointer to the hash table being iterated
  106. public:
  107. HashTableIterator(HashTable<Data> * hashTable);
  108. bool next(const char * & key, Data & data);
  109. };
  110.  
  111. template <typename Data>
  112. HashTableIterator<Data>::HashTableIterator(HashTable<Data> * hashTable)
  113. {
  114. // TODO: Initialize iterator. "hashTable" is the table to be iterated.
  115. }
  116.  
  117. template <typename Data>
  118. bool HashTableIterator<Data>::next(const char * & key, Data & data)
  119. {
  120. // TODO: Returns the next element in the hash table.
  121. // The key and data values are stored in the references passed
  122. // as argument. It returns true if there are more entries or
  123. // false otherwise.
  124. return false;
  125. }
Reply With Quote Quick reply to this message  
Join Date: Nov 2005
Posts: 251
Reputation: dwks has a spectacular aura about dwks has a spectacular aura about 
Solved Threads: 25
dwks's Avatar
dwks dwks is offline Offline
Posting Whiz in Training

Re: Project help

 
0
  #2
Feb 14th, 2007
dwk

Seek and ye shall find.

"Only those who will risk going too far can possibly find out how far one can go."
-- TS Eliot.

"I have not failed. I've just found 10,000 ways that won't work."
-- Thomas Alva Edison

"The only real mistake is the one from which we learn nothing."
-- John Powell
Reply With Quote Quick reply to this message  
Join Date: Feb 2007
Posts: 10
Reputation: zakula11 is an unknown quantity at this point 
Solved Threads: 0
zakula11 zakula11 is offline Offline
Newbie Poster

Re: Project help

 
0
  #3
Feb 26th, 2007
Thanks. Those links helped me alot. I got the class implemented (except for the iterator part... I was still unable to figure that out.), and was able to pass 7 of the 9 test cases (the other two involved the iterator).

However, although I got it to pass the testcases, I am still not positive that I implemented it correctly. The implementation of a hash table will also be important for my new current project (building a webcrawler) and future projects (building a search engine).
So I was wondering if you can look at my implementation and tell me if I did it correctly, and also how to implement the iterator.

Thanks for the help.

Here is my updated implementation:


  1.  
  2.  
  3. //
  4. // CS251 Data Structures
  5. // Hash Table template
  6. //
  7. #include <assert.h>
  8. #include <stdlib.h>
  9. #include <string.h>
  10. ///////////////////////////////////////////
  11. //
  12. // Class that defines a template for a hash table
  13. // The keys are of type const char * and the data
  14. // can be any type.
  15. //
  16. // Use like this:
  17. // HashTable hashTableInt<int>;
  18. //
  19. //
  20. ///////////////////////////////////////////
  21.  
  22. // Each hash entry stores a key, object pair
  23. template <typename Data>
  24. struct HashTableEntry {
  25. const char * _key;
  26. Data _data;
  27. HashTableEntry * _next;
  28. };
  29.  
  30. // This is a Hash table that maps string keys to objects of type Data
  31. template <typename Data>
  32. class HashTable {
  33. public:
  34. // Number of buckets
  35. enum { TableSize = 2039};
  36.  
  37. // Array of the hash buckets.
  38. HashTableEntry<Data> **_buckets;
  39.  
  40. // Obtain the hash code of a key
  41. int hash(const char * key);
  42.  
  43. public:
  44. HashTable();
  45. // Add a record to the hash table. Returns true if key already exists.
  46. // Substitute content if key already exists.
  47. bool insertItem( const char * key, Data data);
  48.  
  49.  
  50. // Find a key in the dictionary and place in "data" the corresponding record
  51. // Returns false if key is does not exist
  52. bool find( const char * key, Data * data);
  53. // Removes an element in the hash table. Return false if key does not exist.
  54. bool removeElement(const char * key);
  55. };
  56.  
  57. template <typename Data>
  58. int HashTable<Data>::hash(const char * key)
  59. {
  60. // TODO: Compute the hash number from the key string
  61. int sum=0;
  62. int len = strlen(key);
  63. for(int i=0; i < len; i++)
  64. {
  65. sum = sum + key[i];
  66. }
  67. return sum % TableSize;
  68. }
  69.  
  70.  
  71. template <typename Data>
  72. HashTable<Data>::HashTable()
  73. {
  74. // TODO: Initialize the hash table
  75. _buckets = new HashTableEntry<Data> * [TableSize];
  76. for(int i=0; i < TableSize; i++)
  77. {
  78. _buckets[i] = NULL;
  79. }
  80. }
  81.  
  82. template <typename Data>
  83. bool HashTable<Data>::insertItem( const char * key, Data data)
  84. {
  85. // TODO: Insert a key,data pair inside the hash table.
  86. // Return true if the entry already exists or false if
  87. // it does not exist.
  88. int i = hash(key);
  89. HashTableEntry<Data> *e = _buckets[i];
  90.  
  91. while(e != NULL && strcmp(key, e->_key) != 0)
  92. {
  93. e = e->_next;
  94. }
  95.  
  96. if(e != NULL)
  97. {
  98. //key exists
  99. e->_data = data;
  100. return true;
  101. }
  102.  
  103. //key does not exist
  104. e = new HashTableEntry<Data>;
  105. assert(e != NULL);
  106.  
  107. e->_key = key;
  108. e->_data = data; //add entry to beg. of list
  109. e->_next = _buckets[i];
  110. _buckets[i] = e;
  111. return false;
  112. }
  113.  
  114.  
  115. template <typename Data>
  116. bool HashTable<Data>::find( const char * key, Data * data)
  117. {
  118. // TODO: Find the data associated to a key. The data is
  119. // stored in the *data passed as parameter.
  120. // Return true if the entry is found or false otherwise.
  121. int i = hash(key);
  122. HashTableEntry<Data> *e = _buckets[i];
  123.  
  124. while(e != NULL && strcmp(key, e->_key) != 0)
  125. {
  126. e = e->_next;
  127. }
  128.  
  129. //key is found
  130. if(e != NULL)
  131. {
  132. *data = e->_data;
  133. return true;
  134. }
  135.  
  136. //key was not found
  137. return false;
  138. }
  139.  
  140. template <typename Data>
  141. bool HashTable<Data>::removeElement(const char * key)
  142. {
  143. // TODO: Remove the element that has this key from the hash table.
  144. // Return true if the entry is found or false otherwise.
  145. int i = hash(key);
  146. HashTableEntry<Data> *eC = _buckets[i];
  147. HashTableEntry<Data> *eP = NULL;
  148.  
  149. while(eC != NULL && strcmp(key, eC->_key) != 0)
  150. {
  151. eP = eC;
  152. eC = eC->_next;
  153. }
  154.  
  155. //key is found
  156. if(eC != NULL)
  157. {
  158. if(eP != NULL)
  159. eP->_next = eC->_next;
  160. else
  161. _buckets[i] = eC->_next;
  162. return true;
  163. }
  164.  
  165. //key is not found
  166. return false;
  167. }
  168.  
  169.  
  170. /////////////////////////////////////
  171. //
  172. // Class used to iterate over the hash table
  173. //
  174. /////////////////////////////////////
  175. template <typename Data>
  176. class HashTableIterator {
  177. int _currentBucket; // Current bucket that is being iterated
  178. HashTableEntry<Data> *_currentEntry; // Current entry iterated
  179. HashTable<Data> * _hashTable; // Pointer to the hash table being iterated
  180. public:
  181. HashTableIterator(HashTable<Data> * hashTable);
  182. bool next(const char * & key, Data & data);
  183. };
  184.  
  185. template <typename Data>
  186. HashTableIterator<Data>::HashTableIterator(HashTable<Data> * hashTable)
  187. {
  188. // TODO: Initialize iterator. "hashTable" is the table to be iterated.
  189. _hashTable = hashTable;
  190. }
  191.  
  192. template <typename Data>
  193. bool HashTableIterator<Data>::next(const char * & key, Data & data)
  194. {
  195. // TODO: Returns the next element in the hash table.
  196. // The key and data values are stored in the references passed
  197. // as argument. It returns true if there are more entries or
  198. // false otherwise.
  199. return false;
  200. }
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