Guest User

Untitled

a guest
Apr 13th, 2017
31
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.80 KB | None | 0 0
  1. #include <iostream>
  2. #include <string>
  3.  
  4. #define ARRAY_SIZE(a) (sizeof(a)/sizeof(a[0]))
  5.  
  6. using namespace std;
  7. // Declare the struct
  8.  
  9. #define EMPTY_STUDENT_ID 0 // An Id of 0 means an empty record.
  10. struct Student
  11. {
  12. int id;
  13. std::string name;
  14. std::string telephone;
  15. std::string email;
  16.  
  17. Student* pNext;
  18. };
  19.  
  20. /*
  21. The HashTable is a structure of 2 elements, the first element records the size of the prime area
  22. and the second element is a pointer to the start of the prime area.
  23. */
  24. struct HashTable
  25. {
  26. int size; /* the size of the table */
  27. Student *primeArea; /* the table elements */
  28. };
  29.  
  30. HashTable *my_hash_table;
  31. int size_of_table = 12;
  32.  
  33.  
  34. // Create an array of students.
  35. // These will be put into a hash table
  36. Student studentList[] =
  37. {
  38. {379452, "Mary Odd", "5555555", "mary@gmail.com" },
  39. {367173, "Sarah Alice", "5555555", "sarah@gmail.com" },
  40. {166702, "Harry Joy", "5555555", "harry@gmail.com" },
  41. {572556, "Hidayah", "5555555", "harry@gmail.com"},
  42. };
  43.  
  44. HashTable* create_hash_table(int size)
  45. {
  46. HashTable* new_table;
  47.  
  48. if (size<1) return NULL; /* invalid size for table */
  49.  
  50. // Allocate the hash table main structure
  51. if ((new_table = new HashTable) == NULL) {
  52. return NULL;
  53. }
  54.  
  55. // Allocate the primeArea
  56. if ((new_table->primeArea = new Student[size]) == NULL) {
  57. return NULL;
  58. }
  59.  
  60. /* Initialize the elements of the table */
  61. for(int i=0; i<size; i++)
  62. {
  63. new_table->primeArea[i].pNext = NULL;
  64. // An id of 0 means an empty record.
  65. new_table->primeArea[i].id = EMPTY_STUDENT_ID;
  66.  
  67. }
  68.  
  69. /* Set the table's size */
  70. new_table->size = size;
  71.  
  72. return new_table;
  73. }
  74.  
  75. // This is a hashing function.
  76. unsigned int moduloDivisionHashFunction(Student *pStudent, unsigned int listSize)
  77. {
  78. return pStudent->id % listSize;
  79. }
  80.  
  81.  
  82. void insert_to_hash_table(HashTable* ht, Student* record)
  83. {
  84.  
  85. //This is where the chaining solution should be taken
  86.  
  87. unsigned int hashValue = moduloDivisionHashFunction(record, my_hash_table->size);
  88. cout<< "Student ID " << record->id << " hashes to " << hashValue << endl;
  89.  
  90. ht->primeArea[hashValue].id = record->id;
  91. ht->primeArea[hashValue].name = record->name;
  92. ht->primeArea[hashValue].telephone= record->telephone;
  93. ht->primeArea[hashValue].email = record->email;
  94.  
  95. }
  96.  
  97. int main(void)
  98. {
  99. cout<<"Modulo-Division Hashing Function"<<endl;
  100.  
  101. my_hash_table = create_hash_table(size_of_table);
  102.  
  103. // go through the studentList using a loop
  104. int studentListSize = ARRAY_SIZE(studentList);
  105. for(int i = 0; i < studentListSize ; i++)
  106. {
  107.  
  108. insert_to_hash_table(my_hash_table, &studentList[i]);
  109. }
  110.  
  111. system("pause");
  112. return 0;
  113. }
Add Comment
Please, Sign In to add comment