Sierra_ONE

OPEN HASHING

Sep 18th, 2024
136
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.34 KB | Source Code | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #define SIZE 10 // Size of the dictionary
  4.  
  5. int hash(int elem) {
  6.     return elem % SIZE;
  7. }
  8.  
  9. void initDic(Dictionary dict) {
  10.     for (int i = 0; i < SIZE; i++) {
  11.         dict[i] = NULL; // Set all cells to NULL (empty)
  12.     }
  13. }
  14.  
  15. void displayDic(Dictionary dict) {
  16.     for (int i = 0; i < SIZE; i++) {
  17.         printf("Group %d: ", i);
  18.         cellPtr temp = dict[i];
  19.         while (temp != NULL) {
  20.             printf("%5d ", temp->elem); // Print element with padding
  21.             temp = temp->next; // Move to the next node
  22.         }
  23.         printf("\n");
  24.     }
  25. }
  26.  
  27. void insert(Dictionary dict, int elem) {
  28.     int index = hash(elem); // Get the hash value (group number)
  29.    
  30.     // Check if the element already exists
  31.     cellPtr temp = dict[index];
  32.     while (temp != NULL) {
  33.         if (temp->elem == elem) {
  34.             printf("Element %d already exists in the dictionary.\n", elem);
  35.             return;
  36.         }
  37.         temp = temp->next;
  38.     }
  39.    
  40.     // Insert the element at the start of the linked list (chaining)
  41.     cellPtr newNode = (cellPtr)malloc(sizeof(struct cell));
  42.     newNode->elem = elem;
  43.     newNode->next = dict[index];
  44.     dict[index] = newNode;
  45. }
  46.  
  47.  
  48. void populateDic(Dictionary dict, int elements[], int size) {
  49.     for (int i = 0; i < size; i++) {
  50.         insert(dict, elements[i]);
  51.     }
  52. }
  53.  
  54. void deleteElement(Dictionary dict, int elem) {
  55.     int index = hash(elem); // Get the hash value (group number)
  56.     cellPtr temp = dict[index], prev = NULL;
  57.  
  58.     while (temp != NULL) {
  59.         if (temp->elem == elem) {
  60.             if (prev == NULL) {
  61.                 dict[index] = temp->next; // Remove head of the list
  62.             } else {
  63.                 prev->next = temp->next; // Bypass the node
  64.             }
  65.             free(temp); // Free memory
  66.             printf("Element %d removed from the dictionary.\n", elem);
  67.             return;
  68.         }
  69.         prev = temp;
  70.         temp = temp->next;
  71.     }
  72.     printf("Element %d not found in the dictionary.\n", elem);
  73. }
  74.  
  75. int isMember(Dictionary dict, int elem) {
  76.     int index = hash(elem);
  77.     cellPtr temp = dict[index];
  78.  
  79.     while (temp != NULL) {
  80.         if (temp->elem == elem) {
  81.             return 1; // Element found
  82.         }
  83.         temp = temp->next;
  84.     }
  85.     return 0; // Element not found
  86. }
  87.  
  88.  
Tags: dsa
Advertisement
Add Comment
Please, Sign In to add comment