Advertisement
_Black_Panther_

Hash_Primary_Code

Aug 20th, 2019
292
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.62 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <string.h>
  3. #include <stdlib.h>
  4. #include <stdbool.h>
  5.  
  6. #define SIZE 20
  7.  
  8. struct DataItem {
  9.    int data;
  10.    int key;
  11. };
  12.  
  13. struct DataItem* hashArray[SIZE];
  14. struct DataItem* dummyItem;
  15. struct DataItem* item;
  16.  
  17. int hashCode(int key) {
  18.    return key % SIZE;
  19. }
  20.  
  21. struct DataItem *search(int key) {
  22.    //get the hash
  23.    int hashIndex = hashCode(key);
  24.  
  25.    //move in array until an empty
  26.    while(hashArray[hashIndex] != NULL) {
  27.  
  28.       if(hashArray[hashIndex]->key == key)
  29.          return hashArray[hashIndex];
  30.  
  31.       //go to next cell
  32.       ++hashIndex;
  33.  
  34.       //wrap around the table
  35.       hashIndex %= SIZE;
  36.    }
  37.  
  38.    return NULL;
  39. }
  40.  
  41.  
  42.  
  43. void insert(int key,int data) {
  44.  
  45.    struct DataItem *item = (struct DataItem*) malloc(sizeof(struct DataItem));
  46.    item->data = data;
  47.    item->key = key;
  48.  
  49.    //get the hash
  50.    int hashIndex = hashCode(key);
  51.  
  52.    //move in array until an empty or deleted cell
  53.    while(hashArray[hashIndex] != NULL && hashArray[hashIndex]->key != -1) {
  54.       //go to next cell
  55.       ++hashIndex;
  56.  
  57.       //wrap around the table
  58.       hashIndex %= SIZE;
  59.    }
  60.  
  61.    hashArray[hashIndex] = item;
  62. }
  63.  
  64.  
  65.  
  66. struct DataItem* delete(struct DataItem* item) {
  67.    int key = item->key;
  68.  
  69.    //get the hash
  70.    int hashIndex = hashCode(key);
  71.  
  72.    //move in array until an empty
  73.    while(hashArray[hashIndex] != NULL) {
  74.  
  75.       if(hashArray[hashIndex]->key == key) {
  76.          struct DataItem* temp = hashArray[hashIndex];
  77.  
  78.          //assign a dummy item at deleted position
  79.          hashArray[hashIndex] = dummyItem;
  80.          return temp;
  81.       }
  82.  
  83.       //go to next cell
  84.       ++hashIndex;
  85.  
  86.       //wrap around the table
  87.       hashIndex %= SIZE;
  88.    }
  89.  
  90.    return NULL;
  91. }
  92.  
  93.  
  94.  
  95. void display() {
  96.    int i = 0;
  97.  
  98.    for(i = 0; i<SIZE; i++) {
  99.  
  100.       if(hashArray[i] != NULL)
  101.          printf(" (%d,%d)",hashArray[i]->key,hashArray[i]->data);
  102.       else
  103.          printf(" ~~ ");
  104.    }
  105.  
  106.    printf("\n");
  107. }
  108.  
  109.  
  110.  
  111. int main() {
  112.    dummyItem = (struct DataItem*) malloc(sizeof(struct DataItem));
  113.    dummyItem->data = -1;
  114.    dummyItem->key = -1;
  115.  
  116.    insert(1, 20);
  117.    insert(2, 70);
  118.    insert(42, 80);
  119.    insert(4, 25);
  120.    insert(12, 44);
  121.    insert(14, 32);
  122.    insert(17, 11);
  123.    insert(13, 78);
  124.    insert(37, 97);
  125.  
  126.    display();
  127.    item = search(37);
  128.  
  129.    if(item != NULL) {
  130.       printf("Element found: %d\n", item->data);
  131.    } else {
  132.       printf("Element not found\n");
  133.    }
  134.  
  135.    delete(item);
  136.    item = search(37);
  137.  
  138.    if(item != NULL) {
  139.       printf("Element found: %d\n", item->data);
  140.    } else {
  141.       printf("Element not found\n");
  142.    }
  143. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement