fork download
  1. //Open Addressing
  2. #include<bits/stdc++.h>
  3. using namespace std;
  4.  
  5. struct MyHash
  6. {
  7. int *arr;
  8. int cap,size;
  9.  
  10. MyHash(int c)
  11. {
  12. cap=c;
  13. size=0;
  14. arr=new int[cap];
  15. for(int i=0;i<cap;i++)
  16. arr[i]=-1;
  17. }
  18.  
  19. int hash(int key){
  20. return key%cap;
  21. }
  22. bool insert(int key)
  23. {
  24. if(size==cap)
  25. return false;
  26. int i=hash(key);
  27. while(arr[i]!=-1 && arr[i]!=-2 && arr[i]!=key)
  28. i=(i+1)%cap;
  29. if(arr[i]==key)
  30. return false;
  31. else{
  32. arr[i]=key;
  33. size++;
  34. return true;
  35. }
  36. }
  37. bool search(int key)
  38. {
  39. int h=hash(key);
  40. int i=h;
  41. while(arr[i]!=-1){
  42. if(arr[i]==key)
  43. return true;
  44. i=(i+1)%cap;
  45. if(i==h)
  46. return false;
  47. }
  48. return false;
  49. }
  50. bool erase(int key)
  51. {
  52. int h=hash(key);
  53. int i=h;
  54. while(arr[i]!=-1){
  55. if(arr[i]==key){
  56. arr[i]=-2;
  57. return true;
  58. }
  59. i=(i+1)%cap;
  60. if(i==h)
  61. return false;
  62. }
  63. return false;
  64. }
  65. };
  66.  
  67. int main()
  68. {
  69. MyHash mh(7);
  70. mh.insert(49);
  71. mh.insert(56);
  72. mh.insert(72);
  73. if(mh.search(56)==true)
  74. cout<<"Yes"<<endl;
  75. else
  76. cout<<"No"<<endl;
  77. mh.erase(56);
  78. if(mh.search(56)==true)
  79. cout<<"Yes"<<endl;
  80. else
  81. cout<<"No"<<endl;
  82. }
  83.  
Success #stdin #stdout 0s 5288KB
stdin
Standard input is empty
stdout
Yes
No