Advertisement
Guest User

Untitled

a guest
Jan 25th, 2020
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.23 KB | None | 0 0
  1. #define _CRT_SECURE_NO_WARNINGS
  2. #include <Windows.h>
  3. #include <stdio.h>
  4. #include <math.h>
  5. #include <limits.h>
  6. #define SIZE 3
  7. typedef
  8. struct element
  9. {
  10. char* key;
  11. int value;
  12. struct element* next;
  13. }element;
  14.  
  15.  
  16. element** mas;
  17.  
  18. void storage()
  19. {
  20. mas = (element**)calloc(SIZE, sizeof(element*));
  21. }
  22. int del(char* key)
  23. {
  24. _Bool ans = 0;
  25. unsigned int index = hashFunc(key) % SIZE;
  26. if (strcmp(mas[index]->key, key) == 0)
  27. {
  28. scanf("%i", &ans);
  29. if (ans == 1)
  30. if (mas[index]->next == NULL)
  31. return NULL;
  32. else
  33. {
  34. element* temp = mas[index];
  35. do
  36. {
  37. if (strcmp(temp->next->key, key) == 0)
  38. {
  39. scanf("%i", &ans);
  40. if (ans == 1)
  41. {
  42. if (temp->next->next == NULL)
  43. {
  44. temp->next = NULL;
  45. return NULL;
  46. }
  47. else
  48. {
  49. element* temp2;
  50. do
  51. {
  52. if (temp->next == NULL)
  53. {
  54. temp = NULL;
  55. return;
  56. }
  57. else
  58. {
  59. temp2 = temp->next;
  60. temp->next = temp->next->next;
  61. temp->next->next = temp2;
  62. }
  63. temp2 = temp2->next;
  64. } while (1);
  65. }
  66. }
  67. }
  68. temp = temp->next;
  69. }while (1);
  70. }
  71. }
  72. else
  73. {
  74. element* temp = mas[index];
  75. do
  76. {
  77. if (strcmp(temp->next->key, key) == 0)
  78. {
  79. scanf("%i", &ans);
  80. if (ans == 1)
  81. {
  82. if (temp->next->next == NULL)
  83. {
  84. temp->next = NULL;
  85. return NULL;
  86. }
  87. else
  88. {
  89. element* temp2;
  90. do
  91. {
  92. temp->next = temp->next->next;
  93. temp2 = temp->next->next;
  94. if (temp2->next == NULL)
  95. return NULL;
  96. temp2 = temp2->next;
  97. } while (1);
  98. }
  99. }
  100. }
  101. temp = temp->next;
  102. } while (1);
  103. }
  104. }
  105. element* search(char* key)
  106. {
  107. unsigned int index = hashFunc(key) % SIZE;
  108. if (strcmp(mas[index]->key, key) == 0)
  109. {
  110. return mas[index];
  111. }else
  112. {
  113. element* temp = mas[index];
  114. while (1)
  115. {
  116. if (strcmp(temp->next->key, key) == 0)
  117. {
  118. return temp->next;
  119. }
  120. if (temp->next == NULL)
  121. return NULL;
  122. temp = temp->next;
  123. }
  124.  
  125. }
  126. return mas[index];
  127. }
  128. void push(const char *key, const int value)
  129. {
  130. unsigned int index = hashFunc(key) % SIZE;
  131. if (mas[index] == 0)
  132. {
  133. mas[index] = (element*)calloc(1,sizeof(element));
  134. mas[index]->key = (char*)calloc(strlen(key) + 1, sizeof(char));
  135. strcpy(mas[index]->key, key);
  136. mas[index]->value = value;
  137. }
  138. else
  139. {
  140. element* temp = mas[index];
  141. while (1)
  142. {
  143. if (strcmp(temp->key, key) == 0)
  144. {
  145. temp->value = value;
  146. return;
  147. }
  148. if (temp->next == NULL)
  149. break;
  150. temp = temp->next;
  151. }
  152. temp->next = (element*)calloc(1, sizeof(element));
  153. temp->next->key = (char*)calloc(strlen(key) + 1, sizeof(char));
  154. strcpy(temp->next->key, key);
  155. temp->next->value = value;
  156. }
  157. }
  158.  
  159. unsigned int hashFunc(const char *key)
  160. {
  161. int index = 0, i = 0, k = 63;
  162. while (key[i] != '\0')
  163. {
  164. index = index * k + key[i];
  165. i++;
  166. }
  167. return index;
  168. }
  169.  
  170. int main()
  171. {
  172. storage();
  173. push("apple", 100);
  174. push("hello", 150);
  175. push("student", 30);
  176. element *a = search("student");
  177. printf("%s - %d", a->key, a->value);
  178. del("student");
  179. /*if (a)
  180. printf("%s - %d", a->key, a->value);*/
  181. return 0;
  182. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement