Advertisement
Guest User

Untitled

a guest
Apr 23rd, 2019
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.14 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4. #include <ctype.h>
  5.  
  6. // Uncomment line with "#define DEBUG" if you want to see your hashtable.
  7. // But rememeber to send only solutions with debug turned off!
  8. // #define DEBUG 1
  9.  
  10. #define MAX_CHARS 31
  11.  
  12. typedef unsigned int uint;
  13.  
  14. typedef enum states {
  15. EMPTY = 0,
  16. OCCUPIED = 1,
  17. STH_WAS_HERE = 2
  18. } states;
  19.  
  20. typedef struct Slot {
  21. char name[MAX_CHARS];
  22. char phone[MAX_CHARS];
  23. states state;
  24. } Slot;
  25.  
  26. uint hashfunc(const char* name) {
  27. uint h=0;
  28. int s=773;
  29. for(int i=0; i<strlen(name); i++)
  30. h=h*s+name[i];
  31. return h;
  32. }
  33.  
  34. void add_to_hashtable(Slot** hashtable, int n, const char* name, const char* phone) {
  35. uint hash = hashfunc(name) % n;
  36. int i=0;
  37. while(hashtable[hash]->state == OCCUPIED && i<n)
  38. {
  39. i++;
  40. hash=(hash+i)%n;
  41. }
  42. if(hashtable[hash]->state==OCCUPIED) return;
  43. hashtable[hash]->state = OCCUPIED;
  44. strcpy(hashtable[hash]->name, name);
  45. strcpy(hashtable[hash]->phone, phone);
  46. }
  47.  
  48. char* get_number(Slot** hashtable, int n, const char* name) {
  49. if (n == 0) return NULL;
  50. uint hash = hashfunc(name) % n;
  51. int i=0;
  52. while(hashtable[hash]->state!=EMPTY && strcmp(hashtable[hash]->name, name)!=0 && i<n)
  53. {
  54. i++;
  55. hash=(hash+i)%n;
  56. }
  57. if(hashtable[hash]->state!=OCCUPIED || strcmp(hashtable[hash]->name, name)!=0) return NULL;
  58. return hashtable[hash]->phone;
  59. }
  60.  
  61. void remove_from_hashtable(Slot** hashtable, int n, const char* name) {
  62. uint hash = hashfunc(name) % n;
  63. int i=0;
  64. while(hashtable[hash]->state!=EMPTY && strcmp(hashtable[hash]->name, name)!=0 && i<n)
  65. {
  66. i++;
  67. hash=(hash+i)%n;
  68. }
  69. if(hashtable[hash]->state==EMPTY || strcmp(hashtable[hash]->name, name)!=0) return;
  70. hashtable[hash]->state = STH_WAS_HERE;
  71. }
  72.  
  73.  
  74. void free_memory(Slot** hashtable, int n) {
  75. for (int i = 0; i < n; i++) {
  76. free(hashtable[i]);
  77. }
  78. free(hashtable);
  79. }
  80.  
  81. void debug_print_hashtable(Slot** hashtable, int n) {
  82. #ifdef DEBUG
  83. for (int i = 0; i < n; i++) {
  84. printf("%d: [%d] %s\n", i, hashtable[i]->state, hashtable[i]->name);
  85. }
  86. printf("\n");
  87. #endif
  88. }
  89.  
  90. int main() {
  91. int Z;
  92. scanf("%d", &Z);
  93.  
  94. while (Z--) {
  95. int n, k;
  96. char op;
  97. char tmp_name[MAX_CHARS], tmp_phone[MAX_CHARS];
  98.  
  99. scanf("%d", &n);
  100. scanf("%d", &k);
  101. int size = n * 3;
  102.  
  103. Slot** hashtable = (Slot**)calloc(size, sizeof(Slot*));
  104.  
  105. for (int i = 0; i < size; i++) {
  106. hashtable[i] = (Slot*) malloc(sizeof(Slot));
  107. hashtable[i]->state = EMPTY;
  108. }
  109.  
  110. for (int i = 0; i < k; i++) {
  111. do { scanf("%c", &op); } while(isspace(op));
  112. switch(op) {
  113. case 'a':
  114. scanf("%s", tmp_name);
  115. scanf("%s", tmp_phone);
  116. add_to_hashtable(hashtable, size, tmp_name, tmp_phone);
  117. break;
  118. case 'r':
  119. scanf("%s", tmp_name);
  120. remove_from_hashtable(hashtable, size, tmp_name);
  121. break;
  122. case 'g':
  123. scanf("%s", tmp_name);
  124. char* num = get_number(hashtable, size, tmp_name);
  125. printf("%s\n", num ? num : "");
  126. break;
  127. }
  128. debug_print_hashtable(hashtable, size);
  129. }
  130. free_memory(hashtable, size);
  131. }
  132. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement