SHARE
TWEET

HASH_2.0

allekco May 27th, 2018 57 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, find = -1;
  83.     i = search_number (key);
  84.     node *iter;
  85.     iter = A[i];
  86.     while (iter){
  87.         if (iter->value == key) find = 1;
  88.         iter = iter->next;     
  89.     }
  90.     return find;
  91. }
  92.  
  93. int delete_node (int *A, int key){
  94.    
  95.     int i, done = -1;
  96.     i = search_number (key);
  97.     node *iter;
  98.     iter = A[i];
  99.     iter->early = NULL;
  100.     while (1){
  101.         if (iter->value == key){
  102.             done = 1;
  103.             break;
  104.         }
  105.         iter = iter->next;         
  106.     }
  107.     node *temporary;
  108.     temporary = iter;
  109.     if ((iter->early == NULL)&&(iter->next == NULL)){ //first element and last
  110.         A[i] = NULL;
  111.         free (temporary);
  112.         return done;   
  113.     }
  114.     if ((iter->early != NULL)&&(iter->next != NULL)){ //in the middle
  115.         node *tmp;
  116.         tmp = iter->next;
  117.         iter->next->early = iter->early;
  118.         iter->early->next = tmp;
  119.         free (temporary);
  120.         return done;
  121.     }
  122.     if ((iter->early == NULL)&&(iter->next != NULL)){
  123.         A[i] = iter->next;
  124.         iter->next = NULL;
  125.         free (temporary);  
  126.         return done;
  127.     }
  128.     if ((iter->early != NULL)&&(iter->next == NULL)){
  129.         iter->early->next = NULL;
  130.         free (temporary);
  131.         return done;
  132.     }
  133. }
  134.  
  135. int main (void){
  136.     int count = 30;
  137.     int number, key, key2, i;
  138.     int *A[m];
  139.    
  140.     for (i = 0; i < m; i++){
  141.         A[i] = NULL;
  142.     }
  143.    
  144.     FILE *mf;
  145.     printf ("opening file :");
  146.     mf = fopen ("C:\\Users\\Anna\\Documents\\ci\\hash.dat","r+");
  147.     if (mf == NULL)
  148.         printf ("error\n");
  149.     else printf ("done\n");
  150.    
  151.     for (i = 0; i < count; i++){
  152.         fscanf(mf, "%d %d", &number, &key);
  153.         printf ("%d\n", key);  
  154.     //  printf ("%d\n", j);
  155.         insert_node (A, key);
  156.     }
  157.     printf ("\n");
  158.  
  159.     int variety, search;
  160.     int behavior = 1;
  161.     while (behavior){
  162.         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");
  163.         printf ("Your choice: ");
  164.         scanf ("%d", &variety);
  165.         switch (variety){
  166.         case 1:
  167.             count++;
  168.             printf ("Input key, what we want insert: ");
  169.             scanf ("%d", &key);
  170.             insert_node (A, key);
  171.             printf ("let's see what lye in A if key = %d\n", key);
  172.             output_list (A, key);
  173.             break;
  174.         case 2:
  175.             printf ("Input key, which we search: ");
  176.             scanf ("%d", &key);
  177.             search = search_key (A, key);
  178.             if (search == -1){
  179.                 printf ("no this key in hash-table\n");
  180.             }  
  181.             else {
  182.                 printf ("U have this key in hash-table\n");
  183.                 output_list (A, key);
  184.             }
  185.             break;
  186.         case 3:
  187.             printf ("Input key, which delete: ");
  188.             scanf ("%d", &key2);
  189.             search = search_key(A, key2);
  190.             if (search == -1){
  191.                 printf ("no this key in hash-table\n");
  192.             }  
  193.             else {
  194.                 int del;
  195.                 del = delete_node (A, key2);
  196.                 if (del == -1)
  197.                     printf ("Error. This element wasn't deleted\n");
  198.                 else {
  199.                     printf ("Good job. Element was deleted\n");
  200.                 }
  201.             }  
  202.             break;
  203.         case 4:
  204.             printf ("Max lengh of list %d, this is for this key %d\n", max, max_key);
  205.             printf ("Min lengh of list %d, this is for this key %d\n", min, min_key);
  206.             float average;
  207.             average = (float)count/(float)m;
  208.             printf ("Average lengh of list %f\n", average);
  209.             break;
  210.         case 0:
  211.             behavior = 0;
  212.             break;
  213.         default:
  214.             printf("Wrong input\n" );
  215.             break;
  216.         }
  217.     }
  218.     fclose (mf);
  219.     printf ("file closed\n");
  220.     return 0;
  221. }
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