Advertisement
Guest User

Quadratic probing

a guest
Feb 23rd, 2020
129
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.82 KB | None | 0 0
  1. #include<stdio.h>
  2. #include<stdlib.h>
  3. #define MAX 10
  4.  
  5. int a[MAX];
  6. void qp(int ,int []);
  7. void qpsr(int key,int a[MAX]);
  8. void display(int a[MAX]);
  9.  
  10.  
  11. int main()
  12. {
  13. int i,key,ch;
  14. for(i=0;i<MAX;i++)
  15. a[i]='\0';
  16. do
  17. {
  18. printf("\n \n Program for intersection/searching keys with the quadratic probing");
  19. printf("\n \n ************************************************************\n\n");
  20. printf("\n 1.Insert Keys");
  21. printf("\n 2.Search keys");
  22. printf("\n 3.Display keys");
  23. printf("\n 4.Exit");
  24. printf("\nSelect the operation");
  25. scanf("%d",&ch);
  26. switch(ch)
  27. {
  28. case 1:do{
  29. printf("\nEnter the key value [type-1]for termination");
  30. scanf("%d",&key);
  31. if(key!=-1)
  32. {
  33. qp(key,a);
  34.  
  35. }
  36. }while(key!=-1);
  37. display(a);
  38. break;
  39.  
  40. case 2:printf("\nEnter the search key value");
  41. scanf("%d",&key);
  42. qpsr(key,a);
  43. break;
  44.  
  45. case 3:display(a);
  46. break;
  47.  
  48. default:printf("\nInvalid input given");
  49. }
  50. }while(ch!=4);
  51. }
  52.  
  53. void qp(int key,int a[MAX])
  54. {
  55. int loc;
  56. int i=1;
  57. loc=key%MAX;
  58. while(a[loc]!='\0')
  59. {
  60. loc=(key %MAX+i*i)%MAX;
  61. i++;
  62. }
  63. a[loc]=key;
  64. }
  65.  
  66. void qpsr(int key,int a[MAX])
  67. {
  68. int loc;
  69. loc=key% MAX;
  70. while((a[loc]!=key)&& (a[loc]!=-1))
  71. loc=++loc%MAX;
  72. if(a[loc]!='\0')
  73. printf("\nSearch successful at index %d",loc);
  74. else
  75. printf("\nSearch successful");
  76. }
  77.  
  78. void display(int a[MAX])
  79. {
  80. int i;
  81. printf("\nList of keys('0' indicate that the location is empty):\n");
  82. for(i=0;i<MAX;i++)
  83. {
  84. printf("%d\t",a[i]);
  85. }
  86. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement