Advertisement
Guest User

Untitled

a guest
Feb 23rd, 2020
123
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.47 KB | None | 0 0
  1.  
  2. #include <stdio.h>
  3. #include<stdlib.h>
  4. #define TABLE_SIZE 10
  5.  
  6. int h[TABLE_SIZE]={NULL};
  7.  
  8. void insert()
  9. {
  10.  
  11. int key,index,i,flag=0,hkey;
  12. printf("\n\nenter a value to insert into hash table\n");
  13. scanf("\n%d",&key);
  14. hkey=key%TABLE_SIZE;
  15. for(i=0;i<TABLE_SIZE;i++)
  16. {
  17.  
  18. index=(hkey+i)%TABLE_SIZE;
  19.  
  20. if(h[index] == NULL)
  21. {
  22. h[index]=key;
  23. break;
  24. }
  25.  
  26. }
  27.  
  28. if(i == TABLE_SIZE)
  29.  
  30. printf("\n\nelement cannot be inserted\n");
  31. }
  32. void search()
  33. {
  34.  
  35. int key,index,i,flag=0,hkey;
  36. printf("\n\nenter search element\n");
  37. scanf("\n%d",&key);
  38. hkey=key%TABLE_SIZE;
  39. for(i=0;i<TABLE_SIZE; i++)
  40. {
  41. index=(hkey+i)%TABLE_SIZE;
  42. if(h[index]==key)
  43. {
  44. printf("\nvalue is found at index %d",index);
  45. break;
  46. }
  47. }
  48. if(i == TABLE_SIZE)
  49. printf("\n\n value is not found\n");
  50. }
  51. void display()
  52. {
  53.  
  54. int i;
  55.  
  56. printf("\n\nelements in the hash table are \n");
  57.  
  58. for(i=0;i< TABLE_SIZE; i++)
  59.  
  60. printf("\n\nAt index %d \t value = %d",i,h[i]);
  61.  
  62. }
  63. main()
  64. {
  65. int opt,i;
  66. while(1)
  67. {
  68. printf("\nPress \t1.Insert\t 2.Display\t 3.Search\t 4.Exit \n");
  69. scanf("\n%d",&opt);
  70. switch(opt)
  71. {
  72. case 1:
  73. insert();
  74. break;
  75. case 2:
  76. display();
  77. break;
  78. case 3:
  79. search();
  80. break;
  81. case 4:exit(0);
  82. }
  83. }
  84. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement