Advertisement
GieeF

Haszowanie - Metoda Łańcuchowa

Jan 13th, 2020
114
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.93 KB | None | 0 0
  1. #include <iostream>
  2. #include <cmath>
  3. #include <fstream>
  4.  
  5. using namespace std;
  6.  
  7. class Elem {
  8. private:
  9.     string s;
  10.     Elem* next;
  11.     Elem* prev;
  12. public:
  13.     Elem();
  14.     Elem(string s, Elem* next, Elem* prev);
  15.     string GetString();
  16.     Elem* GetNext();
  17.     void SetNext(Elem* next);
  18.     Elem* GetPrev();
  19.     void SetPrev(Elem* prev);
  20. };
  21.  
  22. Elem::Elem() {
  23.     this->s = "";
  24.     this->next = NULL;
  25.     this->prev = NULL;
  26. }
  27.  
  28. Elem::Elem(string s, Elem* next, Elem* prev) {
  29.     this->s = s;
  30.     this->next = next;
  31.     this->prev = prev;
  32. }
  33.  
  34. string Elem::GetString() {
  35.     return this->s;
  36. }
  37.  
  38. Elem* Elem::GetNext() {
  39.     return this->next;
  40. }
  41.  
  42. void Elem::SetNext(Elem* next) {
  43.     this->next = next;
  44. }
  45.  
  46. Elem* Elem::GetPrev() {
  47.     return this->prev;
  48. }
  49.  
  50. void Elem::SetPrev(Elem* prev) {
  51.     this->prev = prev;
  52. }
  53.  
  54. class DLList {
  55. private:
  56.     Elem* head;
  57. public:
  58.     DLList() { this->head = NULL; }
  59.     void insertFirst(string s);
  60.     void del(string s);
  61.     string locate(string s); //zwraca pusty ciąg jeśli nie istnieje
  62.     friend ostream& operator<<(ostream& out, DLList& l); //wypisuje całą listę
  63. };
  64.  
  65. void DLList::insertFirst(string s) {
  66.     if (head == NULL) {
  67.         head = new Elem(s, NULL, NULL);
  68.     }
  69.     else {
  70.         Elem* newElem = new Elem(s, head, NULL);
  71.         head->SetPrev(newElem);
  72.         head = newElem;
  73.     }
  74. }
  75.  
  76. void DLList::del(string s) {
  77.     Elem* temp = head;
  78.  
  79.     if (temp == NULL) {
  80.         return;
  81.     }
  82.  
  83.     while (temp->GetString() != s) {
  84.         temp = temp->GetNext();
  85.         if (temp == NULL) {
  86.             return;
  87.         }
  88.     }
  89.  
  90.     if (head == temp)
  91.         head = head->GetNext();
  92.  
  93.     Elem* prev = temp->GetPrev();
  94.     Elem* next = temp->GetNext();
  95.  
  96.     if(prev != NULL)
  97.         prev->SetNext(next);
  98.     if(next != NULL)
  99.         next->SetPrev(prev);
  100.  
  101.     delete temp;
  102. }
  103.  
  104. string DLList::locate(string s) {
  105.     Elem* temp = head;
  106.  
  107.     if (temp == NULL) {
  108.         return "";
  109.     }
  110.  
  111.     while (temp->GetString() != s) {
  112.         temp = temp->GetNext();
  113.         if (temp == NULL) {
  114.             return "";
  115.         }
  116.     }
  117.  
  118.     return temp->GetString();
  119. }
  120.  
  121. ostream& operator<<(ostream& out, DLList& l) {
  122.     Elem* temp = l.head;
  123.  
  124.     if (temp == NULL) {
  125.         out << endl;
  126.         return out;
  127.     }
  128.  
  129.     while (temp != NULL) {
  130.         out << temp->GetString() << " ";
  131.         temp = temp->GetNext();
  132.     }
  133.     out << endl;
  134.     return out;
  135. }
  136.  
  137. class HashTable2 {
  138. private:
  139.     DLList* t;
  140.     int capacity;
  141.     int ht_size;
  142. public:
  143.     HashTable2(int c);      //konstruktor tworzący pustą tablicę o pojemności c
  144.     int hashF(string s);        //funkcja haszująca dla ciągu s
  145.     void insert(string s);      //wstawienie ciągu s
  146.     void del(string s);
  147.     string search(string s);    //wyszukuje i zwraca s
  148.     friend ostream& operator<<(ostream& out, HashTable2& ht); //wypisuje tablicę (z numerami pól), pozostawia puste dla wolnych pól
  149. };
  150.  
  151. HashTable2::HashTable2(int c) {
  152.     t = new DLList[c];
  153.     capacity = c;
  154.     ht_size = 0;
  155. }
  156.  
  157. int HashTable2::hashF(string s) {
  158.     unsigned long int wynik = 0;
  159.  
  160.     for (size_t n = 0; n < s.size(); n++) {
  161.         wynik += (unsigned)(s[n] * (int)pow(11, s.size() - n));
  162.         wynik %= capacity;
  163.     }
  164.  
  165.     return (int)wynik;
  166. }
  167.  
  168. void HashTable2::insert(string s) {
  169.     int h = hashF(s);
  170.  
  171.     t[h].insertFirst(s);
  172.     ht_size++;
  173. }
  174.  
  175. void HashTable2::del(string s) {
  176.     int h = hashF(s);
  177.  
  178.     t[h].del(s);
  179.     ht_size--;
  180. }
  181.  
  182. string HashTable2::search(string s) {
  183.     int h = hashF(s);
  184.  
  185.     return t[h].locate(s);
  186. }
  187.  
  188. ostream& operator<<(ostream& out, HashTable2& ht) {
  189.     if (ht.ht_size == 0) {
  190.         return out;
  191.     }
  192.     else {
  193.         for (int i = 0; i < ht.capacity; i++) {
  194.             out << i << ": " << ht.t[i];
  195.         }
  196.     }
  197.     out << endl;
  198.     return out;
  199. }
  200.  
  201. int main() {
  202.     HashTable2 haszowanko(50);
  203.  
  204.     ifstream wejscie("dane.txt");
  205.     string dane;
  206.  
  207.     if (wejscie.good()) {
  208.         cout << "Plik dane.txt otwarty" << endl;
  209.     }
  210.  
  211.     for (int i = 0; i < 40; i++) {
  212.         wejscie >> dane;
  213.         haszowanko.insert(dane);
  214.     }
  215.  
  216.     cout << haszowanko << endl;
  217.  
  218.     haszowanko.del("Liliana");
  219.     haszowanko.del("Igor");
  220.  
  221.     cout << haszowanko << endl;
  222.  
  223.     cout << "Search1: " << haszowanko.search("Jan") << endl;
  224.     cout << "Search2: " << haszowanko.search("KEKW") << endl;
  225.  
  226.  
  227.     return 0;
  228. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement