Advertisement
saltycracker

stringHash.c

Apr 15th, 2020
493
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 5.10 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4. #include <stdbool.h>
  5.  
  6. #define ARR_SIZE 24
  7.  
  8. struct htype {
  9.   int index;
  10.   const char * val;
  11.   struct htype *next;
  12. };
  13.  
  14. unsigned long genHash(const char *str, const size_t size){
  15.   unsigned long hash = 5381;
  16.   int c;
  17.  
  18.   while ((c = *str++))
  19.     hash = ((hash << 5) + hash) + c;
  20.  
  21.   return hash % size;
  22. }
  23.  
  24. void initHashTable(struct htype * ht, const size_t size) {
  25.   size_t i = 0;
  26.   for (i = 0; i < size; i++) {
  27.     ht->index = -1;
  28.     ht->val = NULL;
  29.     ht->next = NULL;
  30.     ++ht;
  31.   }
  32. }
  33.  
  34. struct htype* createHashTable(const size_t size) {
  35.   void * mem = calloc(size, sizeof(struct htype));
  36.   if (mem == NULL) {
  37.     fputs("Memory allocation failed!", stderr);
  38.     exit(0);
  39.   }
  40.   return mem;
  41. }
  42.  
  43. void displayList(const struct htype *h) {
  44.   fprintf(stdout, "\t\t%d %s\n", h->index, h->val);
  45.   if (h->next) {
  46.     displayList(h->next);
  47.   }
  48. }
  49.  
  50. void display(const struct htype * ht, const size_t size) {
  51.   size_t i = 0;
  52.   for (; i < size; ++i) {
  53.     if (ht->val != NULL) {
  54.       fprintf(stdout, "[%ld]: %d, %s, %p\n", i, ht->index, ht->val, (void*)ht->next);
  55.       if (ht->next) {
  56.     displayList(ht->next);
  57.       }
  58.     }
  59.     ht++;
  60.   }
  61. }
  62.  
  63. void addString(struct htype * ht, const char * str, const size_t size) {
  64.   unsigned long hsh = genHash(str, size);
  65.   struct htype * pos = ht + hsh;
  66.   if (pos->index == -1) {
  67.     pos->index = hsh;
  68.     pos->val = str;
  69.   }else {
  70.     if (strcmp(str, pos->val) != 0) {
  71.       struct htype *n = pos->next;
  72.       while (n) {
  73.     if (strcmp(n->val, str) == 0) {
  74.       return;
  75.     }else {
  76.       n = n->next;
  77.     }
  78.       }
  79.       struct htype * nt = (struct htype*)malloc(sizeof(struct htype));
  80.       if (nt == NULL) {
  81.     fputs("malloc failed!\n", stderr);
  82.       }
  83.       struct htype *old = pos->next;
  84.       nt->index = hsh;
  85.       nt->val = str;
  86.       nt->next = old;
  87.       pos->next = nt;
  88.     }
  89.   }
  90. }
  91.  
  92. bool findStr(const struct htype * ht, const char * str, const size_t size) {
  93.   unsigned long hv = genHash(str, size);
  94.   const struct htype * pos = ht + hv;
  95.   if (strcmp(pos->val, str) == 0) {
  96.     return true;
  97.   }else {
  98.     struct htype * n = pos->next;
  99.     while (n) {
  100.       if (strcmp(n->val, str) == 0) {
  101.     return true;
  102.       }
  103.       n = n->next;
  104.     }
  105.   }
  106.   return false;
  107. }
  108.  
  109. void removeStr(struct htype * ht, const char * str, const size_t size) {
  110.   unsigned long hv = genHash(str, size);
  111.   struct htype * pos = ht + hv;
  112.   if (pos->index != -1) {
  113.     if (strcmp(pos->val, str) == 0) {
  114.       if (pos->next) {
  115.     struct htype * nt = pos->next->next;
  116.     pos->index = pos->next->index;
  117.     pos->val = pos->next->val;
  118.     pos->next = nt;
  119.     free(pos->next);
  120.       }else {
  121.     pos->index = -1;
  122.     pos->val = NULL;
  123.       }
  124.       return;
  125.     }
  126.     struct htype * nt = pos;
  127.     while (nt->next) {
  128.       if (strcmp(nt->next->val, str) == 0) {
  129.     struct htype *nn = nt->next->next;
  130.     free(nt->next);
  131.     nt->next = nn;
  132.     return;
  133.       }
  134.       nt = nt->next;
  135.     }
  136.   }
  137. }
  138.  
  139. void freeList(struct htype * ht) {
  140.   if (ht->next) {
  141.     freeList(ht->next);
  142.   }
  143.   free(ht);
  144. }
  145.  
  146. void freeHashTable(struct htype * ht, const size_t size) {
  147.   struct htype * orig = ht;
  148.   size_t i = 0;
  149.   for (; i < size; ++i) {
  150.     if ((ht + i)->next) {
  151.       freeList((ht + i)->next);
  152.     }
  153.   }
  154.   free(orig);
  155. }
  156.  
  157. int main(int argc, char **argv) {
  158.   struct htype * hashTable = createHashTable(ARR_SIZE);
  159.   initHashTable(hashTable, ARR_SIZE);
  160.   addString(hashTable, "Salty", ARR_SIZE);
  161.   addString(hashTable, "Cracker", ARR_SIZE);
  162.   addString(hashTable, "Emily", ARR_SIZE);
  163.   addString(hashTable, "Angela", ARR_SIZE);
  164.   addString(hashTable, "Richard", ARR_SIZE);
  165.   addString(hashTable, "John", ARR_SIZE);
  166.   addString(hashTable, "Betty", ARR_SIZE);
  167.   addString(hashTable, "Zebra", ARR_SIZE);
  168.   addString(hashTable, "Bobby", ARR_SIZE);
  169.   addString(hashTable, "Kenny", ARR_SIZE);
  170.   addString(hashTable, "Brenda", ARR_SIZE);
  171.   addString(hashTable, "Dart", ARR_SIZE);
  172.   addString(hashTable, "Cat", ARR_SIZE);
  173.   addString(hashTable, "Dog", ARR_SIZE);
  174.   addString(hashTable, "Bird", ARR_SIZE);
  175.   addString(hashTable, "Lion", ARR_SIZE);
  176.   addString(hashTable, "Tweety", ARR_SIZE);
  177.   addString(hashTable, "Tank", ARR_SIZE);
  178.   addString(hashTable, "Car", ARR_SIZE);
  179.   addString(hashTable, "Gun", ARR_SIZE);
  180.   addString(hashTable, "Apple", ARR_SIZE);
  181.   addString(hashTable, "Plum", ARR_SIZE);
  182.   addString(hashTable, "Run", ARR_SIZE);
  183.   addString(hashTable, "Peel", ARR_SIZE);
  184.   addString(hashTable, "See", ARR_SIZE);
  185.   addString(hashTable, "Sun", ARR_SIZE);
  186.   addString(hashTable, "Mouse", ARR_SIZE);
  187.   addString(hashTable, "Speaker", ARR_SIZE);
  188.   addString(hashTable, "Key", ARR_SIZE);
  189.   addString(hashTable, "Song", ARR_SIZE);
  190.   addString(hashTable, "Switch", ARR_SIZE);
  191.   addString(hashTable, "On", ARR_SIZE);
  192.   removeStr(hashTable, "Gun", ARR_SIZE);
  193.   removeStr(hashTable, "Apple", ARR_SIZE);
  194.   removeStr(hashTable, "Switch", ARR_SIZE);
  195.   removeStr(hashTable, "Lion", ARR_SIZE);
  196.   display(hashTable, ARR_SIZE);
  197.   removeStr(hashTable, "Gun", ARR_SIZE);
  198.   display(hashTable, ARR_SIZE);
  199.   freeHashTable(hashTable, ARR_SIZE);
  200.   return 0;
  201. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement