944,222 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 517
  • C++ RSS
Nov 3rd, 2009
0

hash table /independent rehaching

Expand Post »
Hi everyone,
Well I come to a point that I have run out of ideas and time and I’m going to ask for help from anyone that can help.
I hope that there will be someone that could give me a hint how to solve my problem.
About my problem I can say that I just received a huge coursework, I was trying to do it my self but now there isn’t much time left till it has to be done and I’m still not confident enough about c++ to be able to implement it my self and I’m not even close to finishing it.

This is what I need to do create a program that demonstrates the use of static Hash Table structures: using ‘independent rehashing’ to resolve
collisions, for constructing a dictionary object, dictionary, with a string ‘word’ and a list
of ‘meaning’s. The default value for a ‘dictionary’ data entry is the NULL string.

Implement the following definition for a C++ class, Meaning which contains facilities for storing and
manipulating the record containing the ‘meaning’ of a ‘word’.

C++ Syntax (Toggle Plain Text)
  1. //Definition file for Meaning.h
  2. #ifndef MEANING
  3. #define MEANING
  4. #include <string>
  5. class Meaning
  6. {
  7. public:
  8. Meaning(); // constructor
  9. ~Meaning(); // destructor
  10. // setter methods
  11. void setMeaning(string);
  12. void setNext(Meaning*);
  13. // getter methods
  14. void printMeaning(); // display the meaning of a word on the screen
  15. string getMeaning(); // return the meaning of a word
  16. Meaning* getNext(); // get the next meaning for a word if it exists
  17. private:
  18. string meaning;
  19. Meaning* next;
  20. };
  21. #endif
Implement the following definition for a C++ class ‘linked list’ which declares facilities
for storing and manipulating a singly linked list of ‘meaning’ nodes.
C++ Syntax (Toggle Plain Text)
  1.  
  2. // Definition file for List.h
  3. #ifndef LIST
  4. Page 3 of 12
  5. #define LIST
  6. #include <string>
  7. #include “Meaning.h”
  8. class List
  9. {
  10. public:
  11. List(); // constructor – initialise empty list
  12. ~List(); // destructor
  13. // check whether the list is empty and return true if empty,
  14. // otherwise return false.
  15. bool isEmpty() const;
  16. // insert meaning at the rear (i.e. end) of the list
  17. void insertAtRear(string meaning);
  18. // display the list contents.
  19. void printList();
  20. private:
  21. Meaning* head; // points to head of list
  22. };
  23. #endif









C++ Syntax (Toggle Plain Text)
  1. //Definition file for IndDicT.h
  2. #ifndef IDT
  3. #define IDT
  4. #include <string>
  5. #include “List.h”
  6. const int MAX_TABLESZ = 23;
  7. Page 6 of 12
  8. class IndDicT
  9. {
  10. public:
  11. IndDicT(); // constructor – create empty table
  12. ~IndDicT(); // destructor
  13. /* This function uses the algorithm in Appendix A to convert the ‘word’ string
  14. into a positive number equivalent. It also uses the ‘division-remainder’
  15. method to divide the numeric value by the MAX_TABLESZ to
  16. return the table index. */
  17. int hashKey(string );
  18. /* The function uses the hashKey() function to find the ‘table index’.
  19. If the ‘word’ is not in the table and the table location given by the
  20. index is empty, the ‘word’ is inserted in the table and its associated
  21. ‘meaning’ is inserted at the end of the list.
  22. If the ‘word’ already exists in the table, the function will only update the
  23. existing linked list by adding the new ‘meaning’ at the end of it. */
  24. void insert(string word, string meaning);
  25. /* return the list of meaning(s) associated with the primary key 'word'.
  26. Return null (i.e. empty) list if the ‘word’ is not present in the table */
  27. List* lookUp( string word );
  28. /* Print the non-empty dictionary entries in alphabetical order in an output
  29. file.
  30. It uses the ‘sort’ algorithm from the Standard Template Library ‘list’ class
  31. to display the ‘word’ and its associated meanings in alphabetical order. */
  32. void traverse();
  33. private:
  34. struct
  35. {
  36. string word;
  37. List* meaningList;
  38. } dictionary[MAX_TABLESZ]; // table to store the word and its meaning(s)
  39. };
  40. #endif

Appendix A Function hashKey()


The function hashKey() converts a given string into a positive numeric value and then
the division-remainder method is applied on the numeric result to return the table index.
C++ Syntax (Toggle Plain Text)
  1. int hashKey(string word)
  2. {
  3. int result = 0;
  4. for (int i=0; i < word.length(); i++)
  5. { // obtain the ASCII integer value of character i in the string ‘word’.
  6. result = result + static_cast<int>(word[i]);
  7. }
  8. // Make sure that the result is a positive numeric value first
  9. return (result % MAX_TABLESZ); // where MAX_TABLESZ is given as 23
  10. }
Appendix B File “listTest.dat” for testing the linked list
“To deliberately act in a way intended to anger someone.”
“In everyday language is a synonym for information.”
“Bachelor of Arts”
“A woodland plant with blue bell-shaped flowers”


Appendix C File “meanings.dat” for testing the Hash Table structures
provoke “To deliberately act in a way intended to anger someone.”
data “In everyday language is a synonym for information.”
BA “Bachelor of Arts”
bluebell “A woodland plant with blue bell-shaped flowers”
wizard “A person who is talented in some specified field.”
provoke “To incite or stimulate.”
B2B “business to business.”
cook To prepare by boiling, baking, frying, etc.
program A list of the acts, speeches, pieces.
Bluetooth “A short-range radio technology that allows wireless communication between
computers, mobile phones, etc.”
sextet “A composition of six instruments.”
multimedia “Refers to the use of electronic media to store and experience multimedia
content.”
wizard “A man in fairy tales who has magical powers.”
provoke “(often foll. by into) to cause a person to react in a particular, often angry, way.”
BA “British Airways”
Page 11 of 12


Appendix D An example of the expected screen output, in order, produced by
running the program using the input file “meanings.dat”
The dictionary is:
BA:
1 “Bachelor of Arts”
2 “British Airways
Bluetooth:
1 “A short-range radio technology that allows wireless communication between
computers, mobile phones, etc.”
bluebell:
1 “A woodland plant with blue bell-shaped flowers”
cook:
1 “To prepare by boiling, baking, frying, etc.”
multimedia
1 “Refers to the use of electronic media to store and experience multimedia content.”
program:
1 “A list of the acts, speeches, pieces.”
provoke
1 “To deliberately act in a way intended to anger someone.”
2 “To incite or stimulate.”
3 “(often foll. by into) to cause a person to react in a particular, often angry, way.”
sextet:
1 “A group of six performers.”
wizard:
1 “A person who is talented in some specified field.”
2 “A man in fairy tales who has magical powers.”
Similar Threads
Reputation Points: 10
Solved Threads: 0
Junior Poster
gedas is offline Offline
177 posts
since Nov 2009

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: Explanation of code line by line
Next Thread in C++ Forum Timeline: Error message "Linker error"????





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


Follow us on Twitter


© 2011 DaniWeb® LLC