Advertisement
Guest User

Untitled

a guest
Apr 8th, 2020
217
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.58 KB | None | 0 0
  1. #include<stdio.h>
  2. #include<string.h>
  3. #include<stdlib.h>
  4.  
  5. //m = table size
  6. int m = 23;
  7.  
  8. struct tnode{
  9.     char value[10];
  10.     int step;
  11. }*ND[25];
  12.  
  13. int hashing(char value[10]){
  14.     //complete the code to do hashing using division hashing function
  15.     //add all the character ASCII in the string
  16.     int key;
  17.     int sum = 0;
  18.     for(int i=0; i<5; i++){
  19.         sum += value[i];
  20.     }
  21.     key = sum % m;
  22.  
  23.     return key;
  24. }
  25.  
  26. struct tnode *newData(char value[10]){
  27.     //complete the code to create a new tnode name ND
  28.     struct tnode *ND = (struct tnode*)malloc(sizeof(struct tnode));
  29.     strcpy(ND->value, value);
  30.     ND->step = 0;
  31.     return ND;
  32. }
  33.  
  34. void insert(char value[10]){
  35.     struct tnode *N_Data = newData(value);
  36.     int key = hashing(value);
  37.     //complete the code to insert new data to hash table. If there is any collision, use linear probing to solve it and show ‘TABLE IS FULL!!!’ when the hash table is full or the data cannot be insert to the table.
  38.  
  39.     if(ND[key]==NULL){
  40.         ND[key] = N_Data;
  41.     }
  42.     else{
  43.         int curr = key;
  44.         while(ND[curr]!=NULL && curr<m){
  45.             curr++;
  46.             N_Data->step = N_Data->step + 1;
  47.         }
  48.  
  49.         if(curr>=m){
  50.             printf("TABLE IS FULL!!!\n");
  51.         }
  52.         else{
  53.             ND[curr] = N_Data;
  54.         }
  55.     }
  56. }
  57.  
  58. void view(){
  59.     for(int i=0;i<m;i++){
  60.         if(ND[i]) printf("[%d]\t%s (%d step(s))\n", i, ND[i]->value, ND[i]->step+1);
  61.         else printf("[%d]\tNULL\n", i);
  62.     }
  63. }
  64.  
  65. int main(){
  66.     for(int i=0;i<m;i++){
  67.         ND[i] = NULL;
  68.     }
  69.  
  70.     insert("AAAAA");
  71.     insert("BBBBB");
  72.     insert("CCCCC");
  73.     insert("AAABB");
  74.     insert("BABAA");
  75.     insert("BAABA");
  76.     insert("BBAAA");
  77.     insert("ABBAA");
  78.  
  79.  
  80.     view();
  81.     return 0;
  82. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement