Advertisement
Joporezka1

Untitled

Jun 4th, 2022
962
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.43 KB | None | 0 0
  1. #include <iostream>
  2. #include <string>
  3. using namespace std;
  4.  
  5. struct node {
  6.         int index;
  7.         string name;
  8.         node* next;
  9.     };
  10.  
  11. class HashTable {
  12. public:
  13.  
  14.  
  15.  
  16.     node* table;
  17.     int size=1000;
  18.  
  19.  
  20.     HashTable() {
  21.         table = new node[size];
  22.     }
  23.  
  24.     int hash_function(int key) {
  25.         return key % 1000;
  26.     }
  27.  
  28.     void add(int key, string name) {
  29.        
  30.  
  31.         int index = hash_function(key);
  32.         node* temp = new node;
  33.         temp->index = key;
  34.         temp->name = name;
  35.        
  36.         if(table[index].next == NULL) {
  37.             table[index].next = temp;
  38.         } else {
  39.             node* temp2 = table[index].next;
  40.             while(temp2->next != NULL) {
  41.                 temp2 = temp2->next;
  42.             }
  43.             temp2->next = temp;
  44.         }
  45.     }
  46.  
  47.     node get(int key,string name) {
  48.         int index = hash_function(key);
  49.         node* temp = table[index].next;
  50.         while(temp != NULL) {
  51.             temp = temp->next;
  52.             if(temp->name== name) {
  53.                 return *temp;
  54.             }
  55.         }
  56.         node nn = {-1, "NOT FOUND", NULL};
  57.         return nn;
  58.     }
  59.  
  60. };
  61.  
  62. void print_node(node n) {
  63.     cout << n.index << " " << n.name << endl;
  64. }
  65.  
  66.  
  67.  
  68. int main()
  69. {
  70.     HashTable ht;
  71.     ht.add(1, "John");
  72.     ht.add(1, "Jane");
  73.     ht.add(1, "Jack");
  74.     ht.add(4, "Jill");
  75.     print_node(ht.get(1, "Jack"));
  76.     return 0;
  77. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement