Advertisement
Guest User

Untitled

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