Advertisement
Diogo03

Hashset

Aug 2nd, 2023 (edited)
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 4.58 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4. #include <stdbool.h>
  5.  
  6. #define fator 0.75
  7. #define capacidade_inicial 7
  8.  
  9. typedef struct Node {
  10.     int data;
  11.     struct Node* next;
  12. } Node;
  13.  
  14. typedef struct {
  15.     Node** table;
  16.     int capacidade;
  17.     int size;
  18. } Hash;
  19.  
  20. Node* cria_no(int valor) {
  21.     Node* novo = (Node*)malloc(sizeof(Node));
  22.     novo->data = valor;
  23.     novo->next = NULL;
  24.     return novo;
  25. }
  26.  
  27. Hash* cria_hash() {
  28.     Hash* hash = (Hash*)malloc(sizeof(Hash));
  29.     hash->table = (Node**)malloc(capacidade_inicial * sizeof(Node*));
  30.     for (int i = 0; i < capacidade_inicial; i++) {
  31.         hash->table[i] = NULL;
  32.     }
  33.     hash->capacidade = capacidade_inicial;
  34.     hash->size = 0;
  35.     return hash;
  36. }
  37.  
  38. int dispercao(int key, int capacidade) {
  39.     return key % capacidade;
  40. }
  41.  
  42. void resize(Hash* hash, int nova_capacidade) {
  43.     Node** newTable = (Node**)malloc(nova_capacidade * sizeof(Node*));
  44.     for (int i = 0; i < nova_capacidade; i++) {
  45.         newTable[i] = NULL;
  46.     }
  47.  
  48.     for (int i = 0; i < hash->capacidade; i++) {
  49.         Node* current = hash->table[i];
  50.         while (current != NULL) {
  51.             Node* temp = current;
  52.             current = current->next;
  53.             int newIndex = dispercao(temp->data, nova_capacidade);
  54.             temp->next = newTable[newIndex];
  55.             newTable[newIndex] = temp;
  56.         }
  57.     }
  58.     free(hash->table);
  59.     hash->table = newTable;
  60.     hash->capacidade = nova_capacidade;
  61. }
  62.  
  63. bool has(Hash* hash, int valor, int* comparisons) {
  64.     int index = dispercao(valor, hash->capacidade);
  65.     Node* current = hash->table[index];
  66.     *comparisons = 1;
  67.     while (current != NULL) {
  68.         if (current->data == valor) {
  69.             return true;
  70.         }
  71.         current = current->next;
  72.         (*comparisons)++;
  73.     }
  74.     return false;
  75. }
  76.  
  77. bool add(Hash* hash, int valor, int* comparisons) {
  78.     int index = dispercao(valor, hash->capacidade);
  79.     Node* novo = cria_no(valor);
  80.  
  81.     if (has(hash, valor, comparisons))
  82.     return false;
  83.  
  84.     novo->next = hash->table[index];
  85.     hash->table[index] = novo;
  86.     hash->size++;
  87.  
  88.     float loadFactor = (float)hash->size / hash->capacidade;
  89.     if (loadFactor > fator) {
  90.         resize(hash, 2 * hash->capacidade - 1);
  91.     }
  92.     return true;
  93. }
  94.  
  95. bool del(Hash* hash, int valor, int* comparisons) {
  96.     int index = dispercao(valor, hash->capacidade);
  97.     Node* current = hash->table[index];
  98.     Node* prev = NULL;
  99.     *comparisons = 1;
  100.     while (current != NULL) {
  101.         if (current->data == valor) {
  102.             if (prev == NULL) {
  103.                 hash->table[index] = current->next;
  104.             } else {
  105.                 prev->next = current->next;
  106.             }
  107.             free(current);
  108.             hash->size--;
  109.             return true;
  110.         }
  111.         prev = current;
  112.         current = current->next;
  113.         (*comparisons)++;
  114.     }
  115.     return false;
  116. }
  117.  
  118. void print(Hash* hash) {
  119.     int max_tam = 0;
  120.     int num_elementos = 0;
  121.     for (int i = 0; i < hash->capacidade; i++) {
  122.         int count = 0;
  123.         Node* current = hash->table[i];
  124.         while (current != NULL) {
  125.             count++;
  126.             current = current->next;
  127.         }
  128.         if (count > max_tam) {
  129.             max_tam = count;
  130.         }
  131.         num_elementos += count;
  132.     }
  133.     printf("%d %d %d\n", hash->capacidade, num_elementos, max_tam);
  134. }
  135.  
  136. void freeHash(Hash* hash) {
  137.     for (int i = 0; i < hash->capacidade; i++) {
  138.         Node* current = hash->table[i];
  139.         while (current != NULL) {
  140.             Node* temp = current;
  141.             current = current->next;
  142.             free(temp);
  143.         }
  144.     }
  145.     free(hash->table);
  146.     free(hash);
  147. }
  148.  
  149. int main() {
  150.     Hash* myTable = cria_hash();
  151.     char comando[4];
  152.     int i = 0;
  153.     while (true) {
  154.         int valor;
  155.         int comparisons = 0;
  156.         if(scanf("%3s", comando) != 1){
  157.             break;
  158.         }
  159.         printf("%d ", i);
  160.         if (!strcmp(comando, "ADD")) {
  161.             scanf("%d",&valor);
  162.             printf("%d %d ", add(myTable, valor, &comparisons), comparisons);
  163.         } else if (!strcmp(comando, "DEL")) {
  164.             scanf("%d",&valor);
  165.             printf("%d %d ", del(myTable, valor, &comparisons), comparisons);
  166.         } else if (!strcmp(comando, "HAS")) {
  167.             scanf("%d",&valor);
  168.             printf("%d %d ", has(myTable, valor, &comparisons), comparisons);
  169.         } else if (!strcmp(comando, "PRT")) {
  170.             print(myTable);
  171.         }
  172.         i++;
  173.         printf("\n");
  174.     }
  175.     freeHash(myTable);
  176.     return 0;
  177. }
  178.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement