Advertisement
majczel23000

Haszowanie Łańcuchowe

Jan 11th, 2018
189
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.32 KB | None | 0 0
  1. #include <iostream>
  2. using namespace std;
  3. const int N = 10;
  4.  
  5. struct lista
  6. {
  7.     string val;
  8.     lista *next;
  9. };
  10.  
  11. lista *H[N];
  12.  
  13. int fH(string s)
  14. {
  15.     int sumaModulo = 0;
  16.     for(int i = 0; i < s.length(); i++)
  17.         sumaModulo += s[i];
  18.     return sumaModulo % 10;
  19. }
  20.  
  21. void wstawianie(string s)
  22. {
  23.     int index = fH(s);
  24.     if(H[index]->val == "") //jesli dodajemy pierwszy element do listy
  25.     {
  26.         lista *p =  new lista;
  27.         p->val = s;
  28.         p->next = NULL;
  29.         H[index] = p;
  30.     }
  31.     else    //jesli juz jakis element jest, to nowy dodajemy na koniec listy
  32.     {
  33.         lista *head = H[index];
  34.         lista *p = new lista;
  35.         p->val = s;
  36.         p->next = NULL;
  37.         while(head->next)
  38.             head = head->next;
  39.         head->next = p;
  40.     }
  41. }
  42.  
  43. int wyszukiwanie(string s)
  44. {
  45.     int index = fH(s);
  46.     if(H[index]->val == "") //jak pusto to  nie ma
  47.         return 0;
  48.     else
  49.     {
  50.         lista *p = H[index];
  51.         for(p ; p != NULL; p = p->next) //przechodzimy liste o numerze index i szukamy
  52.             if(p->val == s) //jesli jest
  53.                 return index;
  54.         return 0;
  55.     }
  56. }
  57.  
  58. void usuwanie(string s)
  59. {
  60.     int index = fH(s);
  61.     lista *prev = H[index];
  62.     lista *tmp = H[index];
  63.     while(tmp != NULL)
  64.     {
  65.         if(tmp->val == s)   //jak wartosc sie zgadza
  66.         {
  67.             if(tmp == H[index]) //jesli usuwamy head z listy
  68.             {
  69.                 H[index] = H[index]->next;
  70.                 delete tmp;
  71.                 tmp = H[index];
  72.                 prev = H[index];
  73.             }
  74.             else    //jesli usuwamy inny niz head
  75.             {
  76.                 prev->next = tmp->next;
  77.                 delete tmp;
  78.                 tmp = prev->next;
  79.             }
  80.         }
  81.         prev = tmp;
  82.         tmp = tmp->next;
  83.     }
  84. }
  85.  
  86. void drukuj()
  87. {
  88.     for(int i = 0; i < N; i++)
  89.     {
  90.         cout<<"["<<i<<"] ";
  91.         for(lista *p = H[i]; p; p = p->next)
  92.             cout << p->val << " ";
  93.         cout << endl;
  94.     }
  95. }
  96.  
  97. int main(){
  98.     //inicjalizacja list
  99.     for(int i = 0; i < N; i++)
  100.     {
  101.         H[i] = new lista;
  102.         H[i]->val = "";
  103.         H[i]->next = NULL;
  104.     }
  105.  
  106.     wstawianie("KOTA");
  107.     wstawianie("BAR");
  108.     wstawianie("ATOK");
  109.     wstawianie("TOKA");
  110.     wstawianie("TO");
  111.     wstawianie("ALA");
  112.     wstawianie("AAL");
  113.     wstawianie("MA");
  114.     wstawianie("AKOT");
  115.     wstawianie("RAB");
  116.     wstawianie("ARB");
  117.     wstawianie("KO");
  118.     wstawianie("RBA");
  119.     drukuj();
  120.  
  121.     cout << wyszukiwanie("KOTA") << endl;
  122.     cout << wyszukiwanie("BAR")<< endl;
  123.     cout << wyszukiwanie("ATOK")<< endl;
  124.     cout << wyszukiwanie("TOKA")<< endl;
  125.     cout << wyszukiwanie("TO")<< endl;
  126.     cout << wyszukiwanie("ALA")<< endl;
  127.     cout << wyszukiwanie("MA")<< endl;
  128.     cout << wyszukiwanie("AKOT")<< endl;
  129.     cout << wyszukiwanie("RAB")<< endl;
  130.     cout << wyszukiwanie("ARB")<< endl;
  131.     cout << wyszukiwanie("KO")<< endl;
  132.     cout << wyszukiwanie("RBA")<< endl;
  133.  
  134.     //usuwanie("ALA");
  135.     usuwanie("KOTA");
  136.     usuwanie("BAR");
  137.     usuwanie("ATOK");
  138.     usuwanie("TOKA");
  139.     usuwanie("TO");
  140.     usuwanie("ALA");
  141.    // usuwanie("MA");
  142.     usuwanie("AKOT");
  143.     usuwanie("RAB");
  144.     usuwanie("ARB");
  145.     //usuwanie("KO");
  146.     //usuwanie("RBA");
  147.     drukuj();
  148.  
  149.  
  150.  
  151.  
  152.  
  153. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement