Advertisement
Guest User

Untitled

a guest
May 22nd, 2019
97
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.28 KB | None | 0 0
  1. #include <stdlib.h>
  2. #include <stdio.h>
  3. #include <string.h>
  4. #include <locale.h>
  5.  
  6. #define HASHTAB_SIZE 256
  7.  
  8. typedef struct listnode {
  9. char *value;
  10.  
  11. struct listnode *next;
  12. }listnode;
  13.  
  14. listnode *hashtab[HASHTAB_SIZE] = {NULL};
  15.  
  16. listnode *hashtab_add (listnode *head, char *key) {
  17. if (head == NULL) {
  18. head = malloc(sizeof(*head));
  19. head->value = key;
  20. head->next = NULL;
  21. printf("Добавлено!\n");
  22. }
  23. else {
  24. listnode *node;
  25. node = malloc(sizeof(*node));
  26. node->value = key;
  27. node->next = head;
  28. head = node;
  29. printf("Добавлено!\n");
  30.  
  31. }
  32. return head;
  33. }
  34.  
  35. listnode *hashtab_delete (listnode *head, char *key) {
  36. if (head == NULL) {
  37. printf("Такого слова нет в словаре, удаление невозможно\n");
  38. return NULL;
  39. }
  40. if (head->value == key) {
  41. listnode *temp = head;
  42. head = temp->next;
  43. free(temp);
  44. printf("Удалено\n");
  45. return head;
  46. }
  47. else {
  48. listnode *prev = head, *curr = head->next;
  49. while (curr->value != key) {
  50. prev = curr;
  51. curr = curr->next;
  52. if (curr == NULL) {
  53. printf("Такого слова нет в словаре, удаление невозможно\n");
  54. return head;
  55. }
  56. }
  57. prev->next = curr->next;
  58. free(curr);
  59. return head;
  60. }
  61. }
  62.  
  63. static const unsigned char T[256] = {
  64. 98, 6, 85,150, 36, 23,112,164,135,207,169, 5, 26, 64,165,219,
  65. 61, 20, 68, 89,130, 63, 52,102, 24,229,132,245, 80,216,195,115,
  66. 90,168,156,203,177,120, 2,190,188, 7,100,185,174,243,162, 10,
  67. 237, 18,253,225, 8,208,172,244,255,126,101, 79,145,235,228,121,
  68. 123,251, 67,250,161, 0,107, 97,241,111,181, 82,249, 33, 69, 55,
  69. 59,153, 29, 9,213,167, 84, 93, 30, 46, 94, 75,151,114, 73,222,
  70. 197, 96,210, 45, 16,227,248,202, 51,152,252,125, 81,206,215,186,
  71. 39,158,178,187,131,136, 1, 49, 50, 17,141, 91, 47,129, 60, 99,
  72. 154, 35, 86,171,105, 34, 38,200,147, 58, 77,118,173,246, 76,254,
  73. 133,232,196,144,198,124, 53, 4,108, 74,223,234,134,230,157,139,
  74. 189,205,199,128,176, 19,211,236,127,192,231, 70,233, 88,146, 44,
  75. 183,201, 22, 83, 13,214,116,109,159, 32, 95,226,140,220, 57, 12,
  76. 221, 31,209,182,143, 92,149,184,148, 62,113, 65, 37, 27,106,166,
  77. 3, 14,204, 72, 21, 41, 56, 66, 28,193, 40,217, 25, 54,179,117,
  78. 238, 87,240,155,180,170,242,212,191,163, 78,218,137,194,175,110,
  79. 43,119,224, 71,122,142, 42,160,104, 48,247,103, 15, 11,138,239
  80. };
  81.  
  82. unsigned int hashtab_hash(char *key) {
  83. unsigned int h = strlen(key) % HASHTAB_SIZE;
  84. char *p;
  85. for (p = key; *p != '\0'; ++p) {
  86. h = T[h ^ (unsigned char)*p];
  87. }
  88. return h;
  89. }
  90.  
  91. void hashtab_lookup (listnode *head, char *key) {
  92. if (head == NULL)
  93. printf ("Такого слова нет в словаре, добавьте его!\n");
  94. else {
  95. if (head->value == key)
  96. printf("Данное слово есть в словаре\n");
  97. else {
  98. if (head->next != NULL) {
  99. listnode *curr = head->next;
  100. while (curr->value != key) {
  101. curr = curr->next;
  102. if (curr == NULL)
  103. printf ("Такого слова нет в словаре, добавьте его!\n");
  104. }
  105. printf ("Данное слово есть в словаре\n");
  106. }
  107. else
  108. printf ("Такого слова нет в словаре, добавьте его!\n");
  109. }
  110. }
  111. }
  112.  
  113. int main() {
  114.  
  115. setlocale(LC_ALL, "Rus");
  116.  
  117. char c;
  118. unsigned char *word;
  119.  
  120. while (1) {
  121.  
  122. c = getchar();
  123. fgets(word, 20, stdin);
  124.  
  125. int index = hashtab_hash(word);
  126.  
  127. if (c == '-')
  128. hashtab[index] = hashtab_delete(hashtab[index], word);
  129. else if (c == '+')
  130. hashtab[index] = hashtab_add(hashtab[index], word);
  131. else if (c == '?')
  132. hashtab_lookup(hashtab[index], word);
  133. else return 0;
  134. }
  135. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement