Advertisement
allekco

hash

Oct 17th, 2019
121
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.31 KB | None | 0 0
  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. int count = 30;
  17.  
  18. typedef struct node{
  19. int value;
  20. int square;
  21. float radical;
  22. struct node *next;
  23. struct node *early;
  24. int position;
  25. }node;
  26.  
  27. int search_number (int key){
  28. int i;
  29. for (i = 0; i < m; i++){
  30. if (i==(((a*key + b)% p)% m)){
  31. return i;
  32. }
  33. }
  34. return -1;
  35. }
  36.  
  37. void insert_node (int *A, int key){
  38. count = count + 1;
  39. int i = search_number (key);
  40. node *tmp = (node*) malloc(sizeof(node));
  41. tmp->value = key;
  42. tmp->square = key*key;
  43. tmp->radical = sqrt((float)key);
  44. if (A[i]==0){
  45. A[i] = tmp;
  46. tmp->next = NULL;
  47. tmp->early = NULL;
  48. tmp->position = 1;
  49. }
  50. else{
  51. node *tmp2;
  52. tmp2 = A[i];
  53. A[i] = tmp;
  54. tmp->next = tmp2;
  55. tmp2->early = tmp;
  56. tmp->position = tmp2->position + 1;
  57. }
  58. if (max < tmp->position){
  59. max = tmp->position;
  60. max_key = tmp->value;
  61. }
  62. }
  63.  
  64. void output_list (int *A, int key){
  65. int j;
  66. j = search_number (key);
  67. if (!A[j]){
  68. printf ("no elements");
  69. }
  70. else{
  71. node *iter = A[j];
  72. while (iter != NULL){
  73. // printf ("The position: %d\n", iter->position );
  74. printf ("Value: %d\n", iter->value);
  75. printf ("The square root of value: %f\n", iter->radical );
  76. printf ("The square of value: %d\n", iter->square );
  77. iter = iter->next;
  78. printf ("\n");
  79. }
  80. }
  81. }
  82.  
  83. int main (void){
  84.  
  85. int number, key, key2, i;
  86. int *A[m];
  87.  
  88. for (i = 0; i < m; i++){
  89. A[i] = NULL;
  90. }
  91.  
  92. FILE *mf;
  93. printf ("opening file :");
  94. mf = fopen ("C:\\Users\\Anna\\Documents\\ci\\hash.dat","r+");
  95. if (mf == NULL)
  96. printf ("error\n");
  97. else printf ("done\n");
  98.  
  99. for (i = 0; i < 30; i++){
  100. fscanf(mf, "%d %d", &number, &key);
  101. printf ("%d\n", key);
  102. // printf ("%d\n", j);
  103. insert_node (A, key);
  104. }
  105. printf ("\n");
  106.  
  107. int variety, search;
  108. int behavior = 1;
  109. while (behavior){
  110. 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");
  111. printf ("Your choice: ");
  112. scanf ("%d", &variety);
  113. switch (variety){
  114. case 1:
  115. printf ("Input key, what we want insert: ");
  116. scanf ("%d", key);
  117. insert_node (A, key);
  118. printf ("let's see what lye in A if key = %d\n", key);
  119. output_list (A, key);
  120. break;
  121. case 2:
  122. printf ("Input key, which we search: ");
  123. scanf ("%d", &key);
  124. search = search_number (key);
  125. if (search == -1){
  126. printf ("no this key in hash-table\n");
  127. }
  128. else {
  129. printf ("U have this key in hash-table\n");
  130. output_list (A, key);
  131. }
  132. break;
  133. case 3:
  134. printf ("Input key, which delete: ");
  135. scanf ("%d", &key2);
  136. // delete_node (A, key2);
  137. output_list (A, key2);
  138. break;
  139. case 4:
  140. printf ("Max lengh of list %d, this is for this key %d\n", max, max_key);
  141. printf ("Min lengh of list %d, this is for this key %d\n", min, min_key);
  142. float average;
  143. average = count/m;
  144. printf ("Average lengh of list %f\n", average);
  145. break;
  146. case 0:
  147. behavior = 0;
  148. break;
  149. default:
  150. printf("Wrong input\n" );
  151. break;
  152. }
  153. }
  154. fclose (mf);
  155. printf ("file closed\n");
  156. return 0;
  157. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement