Advertisement
Guest User

Untitled

a guest
Jun 18th, 2019
100
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.88 KB | None | 0 0
  1. #include "pch.h"
  2. #include <iostream>
  3. #include <vector>
  4. #include <string>
  5.  
  6. using namespace std;
  7.  
  8. typedef struct Node
  9. {
  10. string key;
  11. int value;
  12. Node *next;
  13.  
  14. }Node;
  15.  
  16. int const n = 16;
  17. Node *NodeTab[n];
  18.  
  19. int index(string key, int n)
  20. {
  21. int d = key.size();
  22. int sum = 0;
  23. for (int i = 0; i < d; i++)
  24. {
  25. sum = sum + key[i];
  26. }
  27.  
  28. return sum % n;
  29. }
  30.  
  31. void add(string key, int value)
  32. {
  33. int indeks = index(key, n);
  34.  
  35. if (NodeTab[indeks]->value == -1)
  36. {
  37. NodeTab[indeks]->key = key;
  38. NodeTab[indeks]->value = value;
  39. }
  40. else
  41. {
  42. Node *tempNode = NodeTab[indeks];
  43. while (tempNode->next != NULL)
  44. {
  45. tempNode = tempNode->next;
  46. }
  47. Node *currentNode = new Node;
  48. currentNode->value = value;
  49. currentNode->key = key;
  50. currentNode->next = NULL;
  51. tempNode->next = currentNode;
  52. }
  53. }
  54.  
  55. void show(string key)
  56. {
  57. Node *tempNode = NodeTab[0];
  58.  
  59. int i = 0;
  60. while (i < n)
  61. {
  62. if (key == tempNode->key)
  63. {
  64. cout << tempNode->value;
  65. break;
  66. }
  67. else if (tempNode->next != NULL)
  68. {
  69. tempNode = tempNode->next;
  70. }
  71. else
  72. {
  73. tempNode = NodeTab[++i];
  74. }
  75. }
  76. }
  77.  
  78. int ile(int indeks)
  79. {
  80. Node *tempNode = NodeTab[indeks];
  81. int suma = 0;
  82.  
  83. if (tempNode->value != -1)
  84. {
  85. suma = suma+1;
  86. while (tempNode->next != NULL)
  87. {
  88. tempNode = tempNode->next;
  89. suma++;
  90. }
  91. }
  92. return suma;
  93. }
  94.  
  95. int main()
  96. {
  97. for (int i = 0; i < n; i++)
  98. {
  99. NodeTab[i] = new Node;
  100. NodeTab[i]->key = "-1";
  101. NodeTab[i]->value = -1;
  102. NodeTab[i]->next = NULL;
  103. }
  104.  
  105. int operacja;
  106. cin >> operacja;
  107.  
  108. string key;
  109. int value;
  110. int index;
  111.  
  112. while (operacja != 5)
  113. {
  114. if (operacja == 1)
  115. {
  116. cin >> key >> value;
  117. add(key, value);
  118. }
  119. else if (operacja == 2)
  120. {
  121. cin >> key;
  122. show(key);
  123. }
  124. else if (operacja == 3)
  125. {
  126. cin >> index;
  127. cout << ile(index);
  128. }
  129. cin >> operacja;
  130. }
  131.  
  132. return 0;
  133. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement