document.write('
Data hosted with ♥ by Pastebin.com - Download Raw - See Original
  1. #include <cstdlib>
  2. #include <cstdio>
  3. #include <iostream>
  4. const int B = 65;
  5. typedef char * elementtype;
  6. struct celltype
  7. {
  8. elementtype element;
  9. celltype * next;
  10. };
  11. typedef celltype * position;
  12. class dictionary
  13. {
  14. protected:
  15. position D[B];//tablica nagłówków list
  16. public:
  17. dictionary();
  18. ~dictionary();
  19. void Makenul();
  20. bool Member(elementtype x);
  21. void Insert(elementtype x);
  22. void Delete(elementtype x);
  23. int H(elementtype x);
  24. void print( );
  25. };
  26.  
  27. dictionary::dictionary()
  28. {
  29. for (int i = 0; i<B; i++)
  30. D[i] = NULL;
  31. }
  32.  
  33. dictionary::~dictionary()
  34. {
  35. position p;
  36. for (int i = 0; i<B; i++)
  37. {
  38. while (D[i] != NULL)
  39. {
  40. p = D[i];
  41. D[i] = D[i]->next;
  42. delete p;
  43. }
  44.  
  45. }
  46. }
  47. void dictionary::Makenul()
  48. {
  49. position p;
  50. for (int i = 0; i<B; i++)
  51. {
  52. while (D[i] != NULL)
  53. {
  54. p = D[i];
  55. D[i] = D[i]->next;
  56. delete p;
  57. }
  58.  
  59. }
  60. }
  61. bool dictionary::Member(elementtype x)
  62. {
  63. position current;
  64. current = D[H(x)];
  65. while (current != NULL)
  66. {
  67. if (current->element == x) return true;
  68. else current = current->next;
  69. }
  70. return false;
  71.  
  72. }
  73.  
  74. void dictionary::Insert(elementtype x)
  75. {
  76. int bucket;
  77. position oldheader;
  78. if (!Member(x))
  79. {
  80. bucket = H(x);
  81. oldheader = D[bucket];
  82. D[bucket] = new celltype;
  83. D[bucket]->element = x;
  84. D[bucket]->next = oldheader;
  85. }
  86. }
  87.  
  88. void dictionary::Delete(elementtype x)
  89. {
  90. position p, aktualny;
  91. int bucket;
  92. bucket = H(x);
  93. if (D[bucket] != NULL)
  94. {
  95. if (D[bucket]->element == x)/*jesli x w pierwszej komorce*/
  96. {
  97. p = D[bucket];
  98. D[bucket] = D[bucket]->next;
  99. delete p;
  100. }
  101. else /*jesli x nie wystepuje w pierwszej komorce*/
  102. {
  103. aktualny = D[bucket];
  104. while (aktualny->next != NULL)
  105. if ((aktualny->next->element) == x)
  106. {
  107. p = aktualny->next;
  108. aktualny->next = aktualny->next->next;
  109. delete p;
  110. break; /*wyjscie z petli po usunieciu*/
  111. }
  112. else aktualny = aktualny->next;
  113. }
  114. }
  115.  
  116. }
  117. int dictionary::H(elementtype x)
  118. {
  119. return (int(x[0])) % B;
  120. }
  121.  
  122. void dictionary::print( ){
  123.  
  124. for (int i = 0; i < B; i++)
  125. {
  126. elementtype a = (elementtype)(D[i]);
  127. std::cout << "D[" << i << "] = ";
  128. if (D[i] != NULL) std::cout <<D[i];
  129. else std::cout << "-";
  130. std::cout << std::endl;
  131. }
  132. }
  133.  
  134. int main(){
  135. /*dane wejsciowe*/
  136. char *a = "a";
  137. char *b = "b";
  138. char *c = "c";
  139. char *d = "d";
  140. char *e = "e";
  141. char *f = "f";
  142. char *g = "g";
  143. char *h = "h";
  144. char *x = "x";
  145. char *elementconienalezy = "o";
  146.  
  147. char *aa = "r";
  148.  
  149. dictionary *dic = new dictionary();
  150. //std::cout << "Sprawdzam czy cos jest w slowniku: " ;
  151.  
  152. std::cout << "Wrzucam do slownika: a,b,c,d,e,f,g,h,x" << std::endl;
  153. dic->Insert(a);
  154. dic->Insert(b);
  155. dic->Insert(c);
  156. dic->Insert(d);
  157. dic->Insert(e);
  158. dic->Insert(f);
  159. dic->Insert(g);
  160. dic->Insert(h);
  161. dic->Insert(x);
  162. dic->Insert(aa);
  163. // std::cout << "Sprawdzam jak wyglada... :\\n ";
  164. //dic->print();
  165.  
  166. std::cout << "Sprawdzam czy a nalezy do slownika... 1 - nalezy | 0 nie nalezy :\\n ";
  167. std::cout << dic->Member(a)<<std::endl;
  168. std::cout << "Sprawdzam czy aa nalezy do slownika... 1 - nalezy | 0 nie nalezy :\\n ";
  169. std::cout << dic->Member(aa) << std::endl;
  170.  
  171. std::cout << "Usuwam a ze slownika \\n ";
  172. dic->Delete(a);
  173. std::cout << "Sprawdzam czy a nalezy do slownika... 1 - nalezy | 0 nie nalezy :\\n ";
  174. std::cout << dic->Member(a) << std::endl;
  175. std::cout << "Sprawdzam czy aa nalezy do slownika... 1 - nalezy | 0 nie nalezy :\\n ";
  176. std::cout << dic->Member(aa) << std::endl;
  177.  
  178. std::cout << "Sprawdzam czy elementconienalezy nie nalezy do slownika... 1 - nalezy | 0 nie nalezy :\\n ";
  179. std::cout << dic->Member(elementconienalezy);
  180.  
  181. getchar();
  182. }
');