Advertisement
Miquel_Fuster

Basic HashMap

Sep 24th, 2023
874
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 3.01 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <stdbool.h>
  4.  
  5. // Declaracion de la estructura de base
  6. typedef struct Node {
  7.     int data;
  8.     struct Node *next;
  9. } Node;
  10.  
  11. // procedimiento de insercion, recibe la lista por referencia
  12. // para poder modificar el valor del apuntador que recibe
  13. void set_insert(Node **list, int value) {
  14.  
  15.     // genera nuevo nodo
  16.     Node *new_node, *last_node, *actual_node;
  17.     new_node = malloc(sizeof(Node));
  18.     if (!new_node) {
  19.         return;
  20.     }
  21.  
  22.     // da un valor a los nuevos nodos
  23.     new_node->data = value;
  24.     new_node->next = NULL;
  25.  
  26.     // en caso de que la lista esté vacía, hacer el nuevo nodo el inicio de la lista
  27.     if (!*list) {
  28.         *list = new_node;
  29.         return;
  30.     }
  31.    
  32.     // en caso de que la lista exista...
  33.     last_node = *list;
  34.     actual_node = (*list)->next;
  35.  
  36.     // impedir duplicados
  37.     if((*list)->data == value) {
  38.         return;
  39.     }
  40.  
  41.     // ... busca el elemento que se mayor o igual al valor pasado por argumento
  42.     while ( (actual_node) && (actual_node->data < value) ) {
  43.         last_node = actual_node;
  44.         actual_node = actual_node->next;
  45.     }
  46.  
  47.     // impedir duplicados
  48.     if ( (actual_node) && (actual_node->data == value) ) {
  49.         return;
  50.     }
  51.  
  52.     // el nuevo elemento es de menor valor al del inicio de la lista, insertarlo antes.
  53.     if (*list == last_node && ((*list)->data > value)) {
  54.         new_node->next = *list;
  55.         *list = new_node;
  56.         return;
  57.     }
  58.  
  59.     // inserción del elemento dentro de la lista
  60.     if (actual_node) { // si no es el final de la lista se inserta entre dos elementos
  61.         new_node->next = actual_node;
  62.     }
  63.  
  64.     last_node->next = new_node;
  65. }
  66.  
  67. void set_free(Node *list) {
  68.     Node *aux;
  69.     while(list) {
  70.         aux = list;
  71.         list = list->next;
  72.         free(aux);
  73.     }
  74. }
  75.  
  76. bool set_has(Node *list, int value) {
  77.     while(list) {
  78.         if(list->data == value) {
  79.             return true;
  80.         }
  81.         list = list->next;
  82.     }
  83.  
  84.     return false;
  85. }
  86.  
  87. void set_show(Node *actual) {  
  88.     while (actual) {
  89.         printf("%d ", actual->data);
  90.         actual = actual->next;
  91.     }
  92. }
  93.  
  94. void hashTable_insert(Node* hashTable[], int value) {
  95.     int index = value % 5;
  96.     set_insert(&hashTable[index], value);
  97. }
  98.  
  99. void hashTable_free(Node* hashTable[]) {
  100.     for(int i = 0; i < 5; ++i) {
  101.         set_free(hashTable[i]);
  102.     }
  103. }
  104.  
  105. bool hashTable_has(Node* hashTable[], int value) {
  106.     int index = value % 5;
  107.     return set_has(hashTable[index], value);
  108. }
  109.  
  110. void hashTable_show(Node* hashTable[]) {
  111.     for(int i = 0; i < 5; ++i) {
  112.         set_show(hashTable[i]);
  113.         puts("");
  114.     }
  115. }
  116.  
  117. int main() {
  118.     Node* hashTable[5] = {0};
  119.  
  120.     for(int i=0; i<25; ++i) {
  121.         hashTable_insert(hashTable, rand() % 100);
  122.     }
  123.  
  124.     hashTable_show(hashTable);
  125.  
  126.     int value = 100;
  127.     printf("El %d esta en la tabla: %s\n", value, hashTable_has(hashTable, value)? "cierto" : "falso");
  128.  
  129.     hashTable_free(hashTable);
  130. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement