Vladislav_Bezruk

Some task

Sep 24th, 2021 (edited)
141
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.78 KB | None | 0 0
  1. #include <iostream>
  2. #include <stdlib.h>
  3. #include <time.h>
  4.  
  5. #define N 45
  6. #define S 30
  7. #define MAX 100
  8.  
  9. using namespace std;
  10.  
  11. struct node {
  12.     int key;
  13.     int value;
  14.     struct node* next = NULL;
  15. };
  16.  
  17. int getRandom();
  18.  
  19. int hash(int key);
  20.  
  21. void push(int key, int value, node* hashTable[]);
  22.  
  23. node* find(int key, node* hashTable[]);
  24.  
  25. void pop(int key, node* hashTable[]);
  26.  
  27. void print(node* hashTable[]);
  28.  
  29. int main() {
  30.     srand(time(NULL));
  31.    
  32.     node* hashTable[S] = { NULL };
  33.    
  34.     for (int i = 0; i < N; i++) {
  35.         int value = getRandom();
  36.        
  37.         push(value, value % 2, hashTable);
  38.     }
  39.    
  40.     cout << "Before removing even elements:" << endl;
  41.    
  42.     print(hashTable);
  43.    
  44.     for (int i = 1; i <= MAX; i++) {
  45.         node* p = find(i, hashTable);
  46.         if (p != NULL && p->value == 0) {
  47.             pop(i, hashTable);
  48.         }
  49.     }
  50.    
  51.     cout << "After removing even elements:" << endl;
  52.    
  53.     print(hashTable);
  54.    
  55.     return 0;
  56. }
  57.  
  58. int getRandom() {
  59.     return rand() % MAX + 1;
  60. }
  61.  
  62. int hash_func(int key) {
  63.     return key % S;
  64. }
  65.  
  66. void push(int key, int value, node* hashTable[]) {
  67.     int hashNumber = hash_func(key);
  68.    
  69.     node* newHashNode = (struct node*)malloc(sizeof(struct node));
  70.    
  71.     newHashNode->key = key;
  72.    
  73.     newHashNode->value = value;
  74.    
  75.     newHashNode->next = NULL;
  76.    
  77.     node* hashNode = hashTable[hashNumber];
  78.    
  79.     if (hashNode == NULL) {
  80.         hashTable[hashNumber] = newHashNode;
  81.         return;
  82.     } else {
  83.         while (hashNode->next != NULL) {
  84.             hashNode = hashNode->next;  
  85.         }
  86.                
  87.         hashNode->next = newHashNode;
  88.     }
  89. }
  90.  
  91. node* find(int key, node* hashTable[]) {
  92.     bool flag = false;
  93.    
  94.     int hashNumber = hash_func(key);
  95.    
  96.     node* hashNode = hashTable[hashNumber];
  97.    
  98.     while (hashNode != NULL) {
  99.         if (hashNode->key == key) {
  100.             return hashNode;
  101.         }
  102.        
  103.         hashNode = hashNode->next;  
  104.     }
  105.    
  106.     return NULL;
  107. }
  108.  
  109. void pop(int key, node* hashTable[]) {
  110.     int hashNumber = hash_func(key);
  111.    
  112.     node* hashNode = hashTable[hashNumber];
  113.    
  114.     while (hashNode != NULL) {
  115.         if (hashNode->key == key) {
  116.             if (hashNode->next == NULL) {
  117.                 hashTable[hashNumber] = NULL;
  118.                 hashNode = NULL;
  119.                
  120.             } else {
  121.                 *hashNode = *hashNode->next;
  122.             }
  123.        
  124.            
  125.             continue;
  126.         }
  127.    
  128.         hashNode = hashNode->next;     
  129.     }
  130. }
  131.  
  132. void print(node* hashTable[]) {
  133.     bool flagTable = false;
  134.    
  135.     cout << endl << "***HASH TABLE***" << endl;
  136.    
  137.     for (int i = 0; i < S; i++) {
  138.         bool flag = false;
  139.        
  140.         node* hashNode = hashTable[i];
  141.        
  142.    
  143.         cout << "i[" << i << "]: ";
  144.        
  145.         while (hashNode != NULL) {
  146.             flag = true;
  147.                    
  148.             flagTable = true;
  149.                    
  150.             cout << hashNode->key << " ";
  151.                
  152.             hashNode = hashNode->next;  
  153.         }
  154.            
  155.         if (flag == false) {
  156.             cout << "NONE";
  157.         }
  158.            
  159.         cout << endl;
  160.     }
  161.        
  162.     if (flagTable == false) {
  163.         cout << "-EMPTY-" << endl;
  164.     }
  165.        
  166.     cout << "*******END*******" << endl << endl;
  167. }
Add Comment
Please, Sign In to add comment