Advertisement
Guest User

linear probing

a guest
May 24th, 2015
195
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.77 KB | None | 0 0
  1. unsigned long* insertLinear(unsigned long* tab, int size, unsigned long value){
  2. int stableIndex = getIndex(value);
  3. int index = stableIndex;
  4. int j = 0;
  5. while (index < size){
  6. if (tab[index] == NULL){
  7. tab[index] = value;
  8. break;
  9. }
  10. else index++;
  11. j++;
  12. }
  13.  
  14. if (tab[index] == NULL){
  15. int i = 0;
  16. while (i < stableIndex){
  17. if (tab[i] == NULL){
  18. tab[i] = value;
  19. break;
  20. }
  21. else i++;
  22. j++;
  23. }
  24. }
  25.  
  26. cout << "linear probing insert (complexity): " << j << endl;
  27. return tab;
  28. }
  29.  
  30. int searchLinear(unsigned long* tab, int size, unsigned long toSearch){
  31. int index = getIndex(toSearch);
  32. int stableIndex = index;
  33. int j = 0;
  34.  
  35. while (index < size){
  36. if (tab[index] == toSearch){ cout << "linear probing search (complexity): " << j << endl; return index; }
  37. else index++;
  38. j++;
  39. }
  40.  
  41. if (tab[index] != toSearch){
  42. int i = 0;
  43. while (i < stableIndex){
  44. if (tab[i] == toSearch){ cout << "linear probing search (complexity): " << j << endl; return i; }
  45. else i++;
  46. j++;
  47. }
  48. }
  49.  
  50. cout << "linear probing search (complexity): " << j << endl;
  51. return -1;
  52. }
  53.  
  54. unsigned long* removeLinear(unsigned long* tab, int size, unsigned long toRemove){
  55. int index = getIndex(toRemove);
  56. int stableIndex = index;
  57. int j = 0;
  58.  
  59. while (index < size){
  60. if (tab[index] == toRemove){
  61. tab[index] = NULL;
  62. while (index < size){
  63. insertLinear(tab, size, tab[index + 1]);
  64. }
  65. }
  66. else index++;
  67. j++;
  68. }
  69.  
  70. if (tab[index] != toRemove){
  71. int i = 0;
  72. while (i < stableIndex){
  73. if (tab[i] == toRemove){
  74. tab[index] = NULL;
  75. while (index < size){
  76. insertLinear(tab, size, tab[index + 1]);
  77. }
  78. }
  79. else i++;
  80. j++;
  81. }
  82. }
  83.  
  84. cout << "linear probing remove (complexity): " << j << endl;
  85. return tab;
  86. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement