Advertisement
Guest User

Untitled

a guest
Apr 7th, 2020
169
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.56 KB | None | 0 0
  1. #include <iostream>
  2. #include <string>
  3. using namespace std;
  4.  
  5. struct Node
  6. {
  7.     string name;
  8.     Node * next;
  9. };
  10.  
  11.  
  12. int hashF(string str)
  13. {
  14.     int temp = 0;
  15.     for(int i = 0; i < str.length(); i++)
  16.     {
  17.         temp+=str[i];
  18.     }
  19.     temp = temp % 11;
  20.     return temp;
  21. }
  22.  
  23. void add(string name, Node* Tab[])
  24. {
  25.     int count = hashF(name);    //zeby nie wywolywac funkcji wiele razy
  26.     if(Tab[count] -> name == "")
  27.     {
  28.       Tab[count]->name = name;
  29.       Tab[count]->next = nullptr;
  30.     }
  31.  
  32.     else
  33.     {
  34.  
  35.         while(Tab[count] -> name != "")
  36.         {
  37.             if(Tab[count] -> next == nullptr)
  38.             {
  39.                 Tab[count] -> next = new Node();
  40.             }
  41.             Tab[count] = Tab[count] -> next;
  42.  
  43.         }
  44.         Tab[count]->name = name;
  45.         cout << Tab[count]->name;
  46.         Tab[count]->next = nullptr;
  47.  
  48.     }
  49. }
  50.  
  51. void find(string name, Node * Tab[])
  52. {
  53.     if(Tab[hashF(name)] -> name == name)
  54.     {
  55.  
  56.       cout << Tab[hashF(name)] -> name << endl;
  57.     }
  58.     else
  59.     {
  60.         int count = hashF(name);
  61.         while(Tab[count] -> name != name)
  62.         {
  63.            Tab[count] = Tab[count] -> next;
  64.         }
  65.         cout << Tab[count]->name << endl;
  66.     }
  67.  
  68. }
  69.  
  70. int main()
  71. {
  72.     Node * Tab[11];
  73.     for(int i = 0; i < 11; i++)
  74.     {
  75.         Tab[i] = new Node();
  76.     }
  77.  
  78.     add("Mia", Tab);
  79.     add("Sue", Tab);
  80.  
  81.     find("Mia", Tab);
  82. //    Tab[4] = Tab[4] -> next;
  83.  //   cout << Tab[4] -> next;
  84.     for(int i = 0; i < 11; i++)
  85.     {
  86.        delete Tab[i];
  87.     }
  88.     return 0;
  89. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement