fork download
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3.  
  4. struct list_struct {
  5. void *value;
  6. struct list_struct *next;
  7. };
  8.  
  9. struct hash_elem {
  10. struct list_struct *values;
  11. struct hash_elem *next;
  12. int key;
  13. };
  14.  
  15. struct hash_table {
  16. struct hash_elem *first;
  17.  
  18. int (*hashFun)(void *);
  19.  
  20. void (*printFun)(void *);
  21. };
  22.  
  23.  
  24. struct hash_table *prepareHashTable(int(*hashFun)(void *), void(*printFun)(void *)) {
  25. struct hash_table *table = (struct hash_table *) malloc(sizeof(struct hash_table));
  26. table->printFun = printFun;
  27. table->hashFun = hashFun;
  28. return table;
  29. }
  30.  
  31. void add(struct hash_table *hashTable, void *value) {
  32. int hashValue = hashTable->hashFun(value);
  33. if (hashTable->first == NULL) {
  34. hashTable->first = (struct hash_elem *) malloc(sizeof(struct hash_elem));
  35. hashTable->first->key = hashValue;
  36. hashTable->first->values = (struct list_struct *) malloc(sizeof(struct list_struct));
  37. hashTable->first->values->value = value;
  38.  
  39. return;
  40. }
  41.  
  42. struct hash_elem *hashElem = hashTable->first;
  43. while (hashElem->key != hashValue) {
  44. if (hashElem->next != NULL)
  45. hashElem = hashElem->next;
  46. else {
  47. hashElem->next = (struct hash_elem *) malloc(sizeof(struct hash_elem));
  48. hashElem->next->key = hashValue;
  49. hashElem = hashElem->next;
  50. }
  51. }
  52.  
  53. if (hashElem->values == NULL) {
  54. hashElem->values = (struct list_struct *) malloc(sizeof(struct list_struct));
  55. hashElem->values->value = value;
  56. return;
  57. }
  58.  
  59. struct list_struct *listElem = hashElem->values;
  60.  
  61. while (listElem->next != NULL) {
  62. listElem = listElem->next;
  63. }
  64. listElem->next = (struct list_struct *) malloc(sizeof(struct list_struct));
  65. listElem->next->value = value;
  66.  
  67. }
  68.  
  69. void deleteAll(struct hash_table *hashTable) {
  70. struct hash_elem *hashElems = hashTable->first;
  71. struct hash_elem *hashElems2;
  72. struct list_struct *values2;
  73. struct list_struct *values;
  74. while (hashElems != NULL) {
  75. hashElems2 = hashElems->next;
  76. values = hashElems->values;
  77. while (values != NULL) {
  78. values2 = values->next;
  79. free(values);
  80. values = values2;
  81. }
  82. free(hashElems);
  83. hashElems = hashElems2;
  84. }
  85. free(hashTable->first);
  86. hashTable->first = NULL;
  87.  
  88. }
  89.  
  90.  
  91. void printValue(struct hash_table *hashTable, void *value) {
  92. int hash = hashTable->hashFun(value);
  93.  
  94. if (hashTable->first == NULL)
  95. return;
  96.  
  97. struct hash_elem *tmp = hashTable->first;
  98. while (tmp->key != hash) {
  99. tmp = tmp->next;
  100. if (tmp == NULL)
  101. return;
  102. }
  103.  
  104. struct list_struct *tmp2 = tmp->values;
  105.  
  106. while (tmp2 != NULL) {
  107. hashTable->printFun(tmp2->value);
  108. tmp2 = tmp2->next;
  109. }
  110. }
  111.  
  112. typedef int my_int;
  113.  
  114. void printInt(void *e) {
  115. my_int *tmp;
  116. tmp = (my_int *) e;
  117. printf("%d ", *tmp);
  118. }
  119.  
  120. int generateHashInt(void *value) {
  121. my_int *tmp;
  122. tmp = (my_int *) value;
  123. return (*tmp) % 2;
  124. }
  125.  
  126. int main() {
  127. struct hash_table *table = prepareHashTable(generateHashInt, printInt);
  128.  
  129. int a = 10;
  130. int b = 11;
  131. int c = 12;
  132.  
  133. add(table, (void *) &a);
  134. add(table, (void *) &b);
  135. add(table, (void *) &c);
  136. printValue(table, (void *) &a);
  137.  
  138. return 0;
  139. }
Success #stdin #stdout 0s 4532KB
stdin
Standard input is empty
stdout
10 12