SHARE
TWEET

HASH_2.1

allekco May 27th, 2018 67 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. }node;
  24.  
  25. int search_number (int key){
  26.     int i;
  27.     for (i = 0; i < m; i++){
  28.         if (i==(((a*key + b)% p)% m)){
  29.             return i;
  30.         }  
  31.     }
  32.     return -1;     
  33. }
  34.  
  35. void insert_node (int *A, int key){
  36.     int i = search_number (key);
  37.     node *tmp = (node*) malloc(sizeof(node));
  38.     tmp->value = key;
  39.     tmp->square = key*key;
  40.     tmp->radical = sqrt((float)key);
  41.     if (A[i] == 0){
  42.         A[i] = tmp;
  43.         tmp->next = NULL;
  44.         tmp->early = NULL;
  45.     }
  46.     else{
  47.         node *tmp2;
  48.         tmp2 = A[i];
  49.         A[i] = tmp;
  50.         tmp->next = tmp2;
  51.         tmp2->early = tmp;
  52.     }
  53. }
  54.  
  55. void output_list (int *A, int key){
  56.     int j;
  57.     j = search_number (key);
  58.     if (!A[j]){
  59.         printf ("no elements");
  60.     }
  61.     else{
  62.         node *iter = A[j];
  63.         while (iter != NULL){
  64.             printf ("Value: %d\n", iter->value);
  65.             printf ("The square root of value: %f\n", iter->radical );
  66.             printf ("The square of value: %d\n", iter->square );
  67.             iter = iter->next;
  68.             printf ("\n");
  69.         }
  70.     }
  71. }
  72.  
  73. int search_key (int *A, int key){
  74.     int i, find = -1;
  75.     i = search_number (key);
  76.     node *iter;
  77.     iter = A[i];
  78.     while (iter){
  79.         if (iter->value == key) find = 1;
  80.         iter = iter->next;     
  81.     }
  82.     return find;
  83. }
  84.  
  85. int delete_node (int *A, int key){
  86.    
  87.     int i, done = -1;
  88.     max = 0;
  89.     max_key = 0;
  90.     i = search_number (key);
  91.     node *iter;
  92.     iter = A[i];
  93.     iter->early = NULL;
  94.     while (1){
  95.         if (iter->value == key){
  96.             done = 1;
  97.             break;
  98.         }
  99.         iter = iter->next;         
  100.     }
  101.     node *temporary;
  102.     temporary = iter;
  103.     if ((iter->early == NULL)&&(iter->next == NULL)){ //first element and last
  104.         A[i] = NULL;
  105.         free (temporary);
  106.         return done;   
  107.     }
  108.     if ((iter->early != NULL)&&(iter->next != NULL)){ //in the middle
  109.         node *tmp;
  110.         tmp = iter->next;
  111.         iter->next->early = iter->early;
  112.         iter->early->next = tmp;
  113.         free (temporary);
  114.         return done;
  115.     }
  116.     if ((iter->early == NULL)&&(iter->next != NULL)){
  117.         A[i] = iter->next;
  118.         iter->next = NULL;
  119.         free (temporary);  
  120.         return done;
  121.     }
  122.     if ((iter->early != NULL)&&(iter->next == NULL)){
  123.         iter->early->next = NULL;
  124.         free (temporary);
  125.         return done;
  126.     }
  127. }
  128.  
  129. void max_list (int *A){
  130.     node *iter;
  131.     int i, count;
  132.     for (i = 0; i < m; i++){
  133.         iter = A[i];
  134.         count = 0; 
  135.         if (iter != NULL){
  136.             while (1){
  137.                 count ++;
  138.                 if (iter->next == NULL)
  139.                     break;
  140.                 iter = iter->next;         
  141.             }
  142.             if (max < count){
  143.                 max = count;
  144.                 max_key = iter->value;
  145.             }  
  146.         }
  147.        
  148.     }
  149. }
  150.  
  151. int main (void){
  152.     int count = 30;
  153.     int number, key, key2, i;
  154.     int *A[m];
  155.    
  156.     for (i = 0; i < m; i++){
  157.         A[i] = NULL;
  158.     }
  159.    
  160.     FILE *mf;
  161.     printf ("opening file :");
  162.     mf = fopen ("C:\\Users\\Anna\\Documents\\ci\\hash.dat","r+");
  163.     if (mf == NULL)
  164.         printf ("error\n");
  165.     else printf ("done\n");
  166.    
  167.     for (i = 0; i < count; i++){
  168.         fscanf(mf, "%d %d", &number, &key);
  169.         printf ("%d\n", key);  
  170.     //  printf ("%d\n", j);
  171.         insert_node (A, key);
  172.     }
  173.     printf ("\n");
  174.  
  175.     int variety, search;
  176.     int behavior = 1;
  177.     while (behavior){
  178.         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");
  179.         printf ("Your choice: ");
  180.         scanf ("%d", &variety);
  181.         switch (variety){
  182.         case 1:
  183.             count++;
  184.             printf ("Input key, what we want insert: ");
  185.             scanf ("%d", &key);
  186.             insert_node (A, key);
  187.             printf ("let's see what lye in A if key = %d\n", key);
  188.             output_list (A, key);
  189.             break;
  190.         case 2:
  191.             printf ("Input key, which we search: ");
  192.             scanf ("%d", &key);
  193.             search = search_key (A, key);
  194.             if (search == -1){
  195.                 printf ("no this key in hash-table\n");
  196.             }  
  197.             else {
  198.                 printf ("U have this key in hash-table\n");
  199.                 output_list (A, key);
  200.             }
  201.             break;
  202.         case 3:
  203.             printf ("Input key, which delete: ");
  204.             scanf ("%d", &key2);
  205.             search = search_key(A, key2);
  206.             if (search == -1){
  207.                 printf ("no this key in hash-table\n");
  208.             }  
  209.             else {
  210.                 int del;
  211.                 del = delete_node (A, key2);
  212.                 if (del == -1){
  213.                     printf ("Error. This element wasn't deleted\n");
  214.                 }
  215.                 else {
  216.                     count--;
  217.                     printf ("Good job. Element was deleted\n");
  218.                 }
  219.             }  
  220.             break;
  221.         case 4:
  222.             max_list (A);
  223.         //  min_list (A);
  224.             printf ("Max lengh of list %d, this is for this key %d\n", max, max_key);
  225.             printf ("Min lengh of list %d, this is for this key %d\n", min, min_key);
  226.             float average;
  227.             average = (float)count/(float)m;
  228.             printf ("Average lengh of list %f\n", average);
  229.             break;
  230.         case 0:
  231.             behavior = 0;
  232.             break;
  233.         default:
  234.             printf("Wrong input\n" );
  235.             break;
  236.         }
  237.     }
  238.     fclose (mf);
  239.     printf ("file closed\n");
  240.     return 0;
  241. }
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
 
Top