Advertisement
Guest User

Untitled

a guest
Apr 21st, 2019
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.48 KB | None | 0 0
  1. /*Program to demostrate Linear probing in
  2. 1. Storing Students' infroamtion
  3. 2. Searching based on roll number.
  4. Note: roll is a 6 digits nubmer ex. 136001
  5. Created by: Dr. Ajit Kumar
  6. Email: ajitkumar.pu@gmail.com
  7. Date: 11-04-2019
  8. */
  9. #include<stdio.h>
  10. #include<string.h>
  11. #include <math.h>
  12. #define MAX 11
  13. #define A 0.6180339887
  14.  
  15. int h(int k, int m)
  16. {
  17. return floor(m * (k*A - floor(k*A)));
  18. }
  19.  
  20. struct student
  21. {
  22. char name[20];
  23. unsigned long roll_no;
  24. float marks;
  25. };
  26. struct student arr_student[MAX];
  27.  
  28. // Linear Probing function
  29. // taking the Array of the student structure as argument
  30. // along with location of collision and hash table size
  31. int linear_probing(struct student hashtable [], int loc, int m)
  32. {
  33. while(loc <= m-1)
  34. {
  35. if(hashtable[loc].roll_no == 0)
  36. {
  37. return loc;
  38. }
  39. loc++;
  40. }
  41. return -1;
  42. }
  43.  
  44. int main()
  45. {
  46.  
  47. int recordCount=0;
  48. int arrayIndex=0;
  49. int i;
  50. unsigned long key_roll=0;
  51. int flag = 1,search_flag =1;
  52.  
  53. while(flag == 1)
  54. {
  55. printf("Enter student roll no: ");
  56. scanf("%u", &key_roll);
  57. //hash calculation using multiplication method
  58. arrayIndex = h(key_roll,11);
  59. printf("%d \t : %d \n", key_roll, arrayIndex);
  60.  
  61. // Checking for hash collision
  62. // default initial value of roll no is use senitel value
  63. // initial value for int in sturcutue is 0
  64.  
  65. if (arr_student[arrayIndex].roll_no != 0)
  66. {
  67. printf("\n\nA hash collision detected\n");
  68. // calling linear probing function to find next free location
  69. arrayIndex = linear_probing(arr_student, arrayIndex, 11);
  70. printf("With Linear probing New location is: %d\n", arrayIndex);
  71. if(arrayIndex == -1)
  72. {
  73. printf("Hash Table Overflow!!\n");
  74. break;
  75. }
  76. }
  77. // using hash value to store a student record
  78. printf("Enter student name: ");
  79. scanf("%s", arr_student[arrayIndex].name);
  80.  
  81. arr_student[arrayIndex].roll_no = key_roll;
  82.  
  83. printf("Enter student marks: ");
  84. scanf("%f", &arr_student[arrayIndex].marks);
  85.  
  86. recordCount++;
  87.  
  88. printf("Do you want to add more entry:(Yes-> 1/No->0)");
  89. scanf("%d",&flag);
  90. }
  91. printf("\n");
  92. printf("\t\tName\tRoll no\tMarks\n");
  93. for(i = 0; i < MAX; i++ )
  94. {
  95. printf("\t \t %s\t%u\t%.2f\n",
  96. arr_student[i].name, arr_student[i].roll_no, arr_student[i].marks);
  97. }
  98. while(search_flag == 1)
  99. {
  100. printf("\n");
  101. // searching a student information based on hash value
  102. printf("Enter Roll number so show the details");
  103. scanf("%u", &key_roll);
  104. //hash calculation using multiplication method
  105. arrayIndex = h(key_roll,10);
  106. //Need to compare the stored value with the search key (roll no)
  107. if(arr_student[arrayIndex].roll_no == key_roll)
  108. {
  109. printf("%s\t%u\t%.2f\n",
  110. arr_student[arrayIndex].name, arr_student[arrayIndex].roll_no,
  111. arr_student[arrayIndex].marks);
  112. }
  113. else
  114. {
  115. for(i = arrayIndex+1; i < MAX; i++ )
  116. {
  117. if (arr_student[i].roll_no == key_roll)
  118. {
  119. printf("%s\t%u\t%.2f\n",
  120. arr_student[i].name, arr_student[i].roll_no, arr_student[i].marks);
  121. break;
  122. }
  123. else
  124. {
  125. if ((arr_student[i].roll_no == 0) || (i == MAX -1 ))
  126. {
  127. printf("Record not found.\n");
  128. break;
  129. }
  130. }
  131. }
  132. }
  133. printf("Do you want search more record:(Yes-> 1/No->0)");
  134. scanf("%d",&search_flag);
  135. }
  136. return 0;
  137. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement