Advertisement
allekco

HASH

May 27th, 2018
118
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.17 KB | None | 0 0
  1.  
  2. #include <stdio.h>
  3. #include <time.h>
  4. #include <stdlib.h>
  5. #include <string.h>
  6. #include <math.h>
  7. #include <stddef.h>
  8.  
  9. int const a = 1;
  10. int const b = 0;
  11. int const p = 89;
  12. int const m = 15;
  13. int max = 0;
  14. int min = 0;
  15. int max_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. void Insert_node (int *A, int i, int key){
  27. node *tmp = (node*) malloc(sizeof(node));
  28. tmp->value = key;
  29. tmp->square = key*key;
  30. tmp->radical = sqrt((float)key);
  31. if (A[i] == NULL){
  32. A[i] = tmp;
  33. tmp->next = NULL;
  34. tmp->early = NULL;
  35. tmp->position = 1;
  36. }
  37. else{
  38. node *tmp2;
  39. tmp2 = A[i];
  40. A[i] = tmp;
  41. tmp->next = tmp2;
  42. tmp2->early = tmp;
  43. tmp->position = tmp2->position + 1;
  44. }
  45. if (max < tmp->position){
  46. max = tmp->position;
  47. max_key = tmp->value;
  48. }
  49. }
  50.  
  51. int search_number (int key){
  52. int i;
  53. for (i = 0; i < m; i++){
  54. if (i==(((a*key + b)% p)% m)){
  55. return i;
  56. }
  57. }
  58. return -1;
  59. }
  60.  
  61. void output_list (int *A, int key){
  62. int j;
  63. j = search_number (key);
  64. if (A[j] == NULL){
  65. printf ("no elements");
  66. }
  67. else{
  68. node *iter = A[j];
  69. while (iter != NULL){
  70. // printf ("The position: %d\n", iter->position );
  71. printf ("Value: %d\n", iter->value);
  72. printf ("The square root of value: %f\n", iter->radical );
  73. printf ("The square of value: %d\n", iter->square );
  74. iter = iter->next;
  75. printf ("\n");
  76. }
  77. }
  78. }
  79.  
  80. int main (void){
  81.  
  82. int number, key, i, j;
  83. int *A[m];
  84.  
  85. for (i = 0; i < m; i++){
  86. A[i] = NULL;
  87. }
  88.  
  89. FILE *mf;
  90. printf ("opening file :");
  91. mf = fopen ("C:\\Users\\Anna\\Documents\\ci\\hash.dat","r+");
  92. if (mf == NULL)
  93. printf ("error\n");
  94. else printf ("done\n");
  95.  
  96. for (i = 0; i < 60; i++){
  97. fscanf(mf, "%d %d", &number, &key);
  98. printf ("%d\n", key);
  99. j = search_number(key);
  100. // printf ("%d\n", j);
  101. Insert_node (A, j, key);
  102. }
  103. printf ("\n");
  104.  
  105. printf ("let's see what lye in A if key = 13\n");
  106. output_list (A, 13);
  107. printf ("let's see what lye in A if key = 109\n");
  108. output_list (A, 109);
  109. printf ("let's see what lye in A if key = 355009\n");
  110. output_list (A, 355009);
  111.  
  112. printf ("\nMax lengh of list %d, This is for this key %d\n", max, max_key);
  113.  
  114. fclose (mf);
  115. printf ("file closed\n");
  116. return 0;
  117. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement