SHARE
TWEET

HASH

allekco May 27th, 2018 63 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <stdio.h>
  2. #include <time.h>
  3. #include <stdlib.h>
  4. #include <string.h>
  5. #include <math.h>
  6. #include <stddef.h>
  7.  
  8. int const a = 1;
  9. int const b = 0;
  10. int const p = 89;
  11. int const m = 15;
  12. int max = 0;
  13. int min = 0;
  14. int max_key = 0;
  15. int min_key = 0;
  16.  
  17. typedef struct node{
  18.     int value;
  19.     int square;
  20.     float radical;
  21.     struct node *next;
  22.     struct node *early;
  23.     int position;
  24. }node;
  25.  
  26. int search_number (int key){
  27.     int i;
  28.     for (i = 0; i < m; i++){
  29.         if (i==(((a*key + b)% p)% m)){
  30.             return i;
  31.         }  
  32.     }
  33.     return -1;     
  34. }
  35.  
  36. void insert_node (int *A, int key){
  37.     int i = search_number (key);
  38.     node *tmp = (node*) malloc(sizeof(node));
  39.     tmp->value = key;
  40.     tmp->square = key*key;
  41.     tmp->radical = sqrt((float)key);
  42.     if (A[i]==0){
  43.         A[i] = tmp;
  44.         tmp->next = NULL;
  45.         tmp->early = NULL;
  46.         tmp->position = 1;
  47.     }
  48.     else{
  49.         node *tmp2;
  50.         tmp2 = A[i];
  51.         A[i] = tmp;
  52.         tmp->next = tmp2;
  53.         tmp2->early = tmp;
  54.         tmp->position = tmp2->position + 1;
  55.     }
  56.     if (max < tmp->position){
  57.         max = tmp->position;
  58.         max_key = tmp->value;
  59.     }
  60. }
  61.  
  62. void output_list (int *A, int key){
  63.     int j;
  64.     j = search_number (key);
  65.     if (!A[j]){
  66.         printf ("no elements");
  67.     }
  68.     else{
  69.         node *iter = A[j];
  70.         while (iter != NULL){
  71.         //  printf ("The position: %d\n", iter->position );
  72.             printf ("Value: %d\n", iter->value);
  73.             printf ("The square root of value: %f\n", iter->radical );
  74.             printf ("The square of value: %d\n", iter->square );
  75.             iter = iter->next;
  76.             printf ("\n");
  77.         }
  78.     }
  79. }
  80.  
  81. int search_key (int *A, int key){
  82.     int i, sight = -1;
  83.     i = search_number (key);
  84.     node *iter;
  85.     iter = A[i];
  86.     while (iter){
  87.         if (iter->value == key) sight = 1;
  88.         iter = iter->next;     
  89.     }
  90.     return sight;
  91. }
  92.  
  93. int main (void){
  94.     int count = 30;
  95.     int number, key, key2, i;
  96.     int *A[m];
  97.    
  98.     for (i = 0; i < m; i++){
  99.         A[i] = NULL;
  100.     }
  101.    
  102.     FILE *mf;
  103.     printf ("opening file :");
  104.     mf = fopen ("C:\\Users\\Anna\\Documents\\ci\\hash.dat","r+");
  105.     if (mf == NULL)
  106.         printf ("error\n");
  107.     else printf ("done\n");
  108.    
  109.     for (i = 0; i < count; i++){
  110.         fscanf(mf, "%d %d", &number, &key);
  111.         printf ("%d\n", key);  
  112.     //  printf ("%d\n", j);
  113.         insert_node (A, key);
  114.     }
  115.     printf ("\n");
  116.  
  117.     int variety, search;
  118.     int behavior = 1;
  119.     while (behavior){
  120.         printf ("Choose what u want:\n1-Add data\n2-Search key \n3-Delete key\n4-Watch on minimal, maximal, average of list\n0-If you want stop this\n");
  121.         printf ("Your choice: ");
  122.         scanf ("%d", &variety);
  123.         switch (variety){
  124.         case 1:
  125.             count++;
  126.             printf ("Input key, what we want insert: ");
  127.             scanf ("%d", &key);
  128.             insert_node (A, key);
  129.             printf ("let's see what lye in A if key = %d\n", key);
  130.             output_list (A, key);
  131.             break;
  132.         case 2:
  133.             printf ("Input key, which we search: ");
  134.             scanf ("%d", &key);
  135.             search = search_key (A, key);
  136.             if (search == -1){
  137.                 printf ("no this key in hash-table\n");
  138.             }  
  139.             else {
  140.                 printf ("U have this key in hash-table\n");
  141.                 output_list (A, key);
  142.             }
  143.             break;
  144.         case 3:
  145.             printf ("Input key, which delete: ");
  146.             scanf ("%d", &key2);
  147.         //  delete_node (A, key2);
  148.             output_list (A, key2);
  149.             break;
  150.         case 4:
  151.             printf ("Max lengh of list %d, this is for this key %d\n", max, max_key);
  152.             printf ("Min lengh of list %d, this is for this key %d\n", min, min_key);
  153.             float average;
  154.             average = (float)count/(float)m;
  155.             printf ("Average lengh of list %f\n", average);
  156.             break;
  157.         case 0:
  158.             behavior = 0;
  159.             break;
  160.         default:
  161.             printf("Wrong input\n" );
  162.             break;
  163.         }
  164.     }
  165.     fclose (mf);
  166.     printf ("file closed\n");
  167.     return 0;
  168. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
Not a member of Pastebin yet?
Sign Up, it unlocks many cool features!
 
Top