Advertisement
Guest User

Hashtabelle mit Linearen sondieren

a guest
Oct 6th, 2018
183
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 3.63 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4. #define N 53
  5. #define h(x) (x % m) //h(x) = x mod m
  6.  
  7. // function declarations
  8. void fill_array(int *A, int table_range, int number);
  9. void insert_table(int *table, int table_range, int hash_index, int val);
  10. void print_table(int*table, int table_range);
  11. int search_table(int *table, int table_range, int val);
  12. void delete(int *table, int table_range, int val);
  13.  
  14. int main(){
  15.    
  16.     int *table; // hashtable
  17.     int i, j, n, choice, index;
  18.     int search, val;
  19.     int num = 0; // insert count (I don't check deletions)
  20.  
  21.     printf("--h(x) = xmod%d--\n", N);
  22.     printf("\n\n");
  23.    
  24.     table = (int*)malloc(N*sizeof(int));
  25.     // initialize to -1, value that represents emptyness
  26.     for(i = 0; i < N; i++){
  27.         table[i] = -1;
  28.     }
  29.    
  30.     while(1){
  31.     printf("1.Insert random numbers\n");
  32.     printf("2.Delete a number\n");
  33.     printf("3.Search a number\n");
  34.     printf("4.Show Hash Table\n");
  35.     printf("0.Exit Programm\n");       
  36.     printf("\n--------\n");
  37.     printf("Choice: ");
  38.     scanf("%d", &choice);
  39.    
  40.     switch(choice){
  41.         case 0: exit(0);       
  42.         case 1:
  43.             // insert random numbers
  44.             do{
  45.                 printf("Lot to insert(<%d): ", N-num);
  46.                 scanf("%d", &n);
  47.             }while(N - num < n);
  48.                
  49.             num += n;
  50.             fill_array(table, N, n);
  51.             printf("Filled Array with %d random values\n", n);
  52.             printf("\n--------\n");
  53.             break;
  54.         case 2:
  55.             // delete a number
  56.             printf("Give a value you want to delete: ");
  57.             scanf("%d",&search);
  58.             delete(table, N, search);
  59.             printf("\n--------\n");
  60.             break;
  61.         case 3:
  62.             // search for a number
  63.             printf("Search for a value: ");
  64.             scanf("%d",&search);
  65.             index=search_table(table, N, search);
  66.             if(index == -1){
  67.                 printf("This value doesn't exist on HashTable!\n");
  68.             }
  69.             else{
  70.                 printf("Value was found at table[%d]\n", index);
  71.             }
  72.             printf("\n--------\n");
  73.             break;
  74.         case 4:
  75.             //print hashtable
  76.             printf("-HASHTABLE-\n\n"); 
  77.             print_table(table, N);
  78.             printf("\n--------\n");
  79.             break;
  80.               }
  81.     }
  82.     return 0;
  83. }
  84.  
  85. // FUNCTIONS
  86. void fill_array(int *A, int table_range, int number){
  87.     int i;
  88.     int num;
  89.     for(i=0; i<number; i++){
  90.     num = rand()%1000; // random number from 0 to 999
  91.     insert_table(A, table_range, num % table_range, num);
  92.     }
  93. }
  94.  
  95. void insert_table(int *table, int table_range, int hash_index, int val){
  96.    
  97.     if(table[hash_index] == -1 && hash_index < table_range){
  98.         table[hash_index] = val;
  99.         return;
  100.     }
  101.     // go to the next index using modulo when it is outside of the array
  102.     hash_index = (hash_index+1) % table_range;
  103.    
  104.     if(hash_index == val % table_range){
  105.     printf("Hashtable is full!\n");
  106.     return;
  107.     }
  108.     insert_table(table, table_range, hash_index, val); 
  109. }
  110.  
  111. void print_table(int *table, int table_range){
  112.     int i;
  113.     for(i=0;i<table_range;i++){
  114.     printf("%d ",table[i]);
  115.     }
  116. }
  117.  
  118. int search_table(int *table, int table_range, int val){
  119.     int i;
  120.     i = val % table_range;
  121.     // we search the whole array starting from the hashindex
  122.     // cause some value in between could have been deleted
  123.     // and we then would have false results
  124.     do{
  125.     if(table[i] == val){
  126.         return i;
  127.     }
  128.     i = (i+1) % table_range;
  129.     }while(i != val % table_range);
  130.     return -1;
  131. }
  132.  
  133. void delete(int *table, int table_range, int val){
  134.     int index;
  135.     // we call the search function to get the index
  136.     index = search_table(table, table_range, val);
  137.     if(index == -1){
  138.         printf("Value %d doesn't exist on HashTable!\n", val);
  139.     return;
  140.     }
  141.     table[index] = -1;
  142.     printf("Value %d was found at table[%d] and was deleted!\n", val, index);
  143. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement