Advertisement
palenda21

Lab17A

Jun 7th, 2020
104
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.77 KB | None | 0 0
  1. #include<iostream>
  2. #include<math.h>
  3.  
  4. using namespace std;
  5.  
  6. struct comp
  7. {
  8.     int data;
  9.     comp* next;
  10. };
  11.  
  12. comp** create(int m)
  13. {
  14.     comp** H = new comp*[m];
  15.     for (int i = 0; i < m; i++) H[i] = NULL;
  16.     return H;
  17. }
  18.  
  19. void add(int data, int m, comp** H)
  20. {
  21.     comp* tmp = new comp;
  22.     tmp->data = data;
  23.     int i = data % m;
  24.     if (H[i] == NULL)
  25.     {
  26.         H[i] = tmp;
  27.         tmp->next = NULL;
  28.     }
  29.     else {
  30.         tmp->next = H[i];
  31.         H[i] = tmp;
  32.     }
  33. }
  34.  
  35. comp* search(int data, int m, comp** H)
  36. {
  37.     int i = abs(data % m);
  38.     comp* tmp = H[i];
  39.     while (tmp != NULL)
  40.     {
  41.         if (tmp->data == data) return tmp;
  42.         tmp = tmp->next;
  43.     }
  44.     return NULL;
  45. }
  46.  
  47. void delet(int m, comp** H)
  48. {
  49.     comp *tmp, *temp;
  50.     for (int i = 0; i < m; i++)
  51.     {
  52.         cout << "H[" << i << "] = ";
  53.         tmp = H[i];
  54.         while (tmp != NULL)
  55.         {
  56.             cout << tmp->data << " ";
  57.             temp = tmp;
  58.             tmp = tmp->next;
  59.             delete temp;
  60.         }
  61.         cout << endl;
  62.     }
  63.     delete[] H;
  64. }
  65.  
  66. int main()
  67.  
  68. {
  69.     int n;
  70.     cout << "n = ";
  71.     cin >> n;
  72.   int* mass = new int[n];
  73.     int m;
  74.     cout << "m = ";
  75.     cin >> m;
  76.     comp** H;
  77.     H = create(m);
  78.     cout << "mass[i] = ";
  79. for (int i = 0; i < n; i++){
  80.    cin >> mass[i];
  81.   }
  82.  
  83. for (int i = 0; i < n; i++)
  84. {
  85. cout << "  " << mass[i];
  86. }
  87.     cout << endl;
  88.     for (int i = 0; i < n; i++)
  89.     {
  90.         add(mass[i], m, H);
  91.     }
  92.    
  93.     int key;
  94.     comp* p;
  95.     cout << "key = ";
  96.     cin >> key;
  97.     while (key != -1)
  98.     {
  99.         p = search(key, m, H);
  100.         if (p == NULL) cout << "There is no element" << endl;
  101.         else cout << p->data << endl;
  102.         cin >> key;
  103.     }
  104.     delet(m, H);
  105. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement