Pabl0o0

Untitled

May 24th, 2017
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.12 KB | None | 0 0
  1. package lab10_final;
  2.  
  3. public class Adresowanie_kwadratowe {
  4.  
  5. private Obiekt[] hashArray;
  6. private int arraySize;
  7.  
  8.  
  9. public Adresowanie_kwadratowe(int size){
  10. arraySize = size;
  11. hashArray = new Obiekt[arraySize];
  12. }
  13.  
  14.  
  15. public void dump(){
  16. System.out.print("Tablica: ");
  17. for(int j=0; j<arraySize; j++){
  18. if(hashArray[j] != null)
  19. System.out.print(hashArray[j].getValue()+" ");
  20. else
  21. System.out.print("* ");
  22. }
  23. System.out.println();
  24. }
  25.  
  26.  
  27.  
  28. public int hashFunction(int key){
  29. return key % arraySize;
  30. }
  31.  
  32.  
  33. public void put(int key, Obiekt ob){
  34. if((double)size()/(double)arraySize < 0.75){
  35. int value = ob.getValue();
  36. int hashVal = hashFunction(key);
  37.  
  38. while(hashArray[hashVal] != null){
  39. ++hashVal;
  40. hashVal %= arraySize;
  41. }
  42. hashArray[hashVal] = ob;
  43. }else{
  44. resize();
  45. put(key, ob);
  46. }
  47. }
  48.  
  49.  
  50.  
  51. public Obiekt get(int key){
  52. int hashVal = hashFunction(key);
  53.  
  54. while(hashArray[hashVal] != null){
  55. if(hashArray[hashVal].getValue() == key)
  56. return hashArray[hashVal];
  57. ++hashVal;
  58. hashVal %= arraySize;
  59. }
  60. return null;
  61. }
  62.  
  63.  
  64. public boolean containsKey(int key){
  65. int hashVal = hashFunction(key);
  66.  
  67. while(hashArray[hashVal] != null){
  68. if(hashArray[hashVal].getValue() == key)
  69. return true;
  70. ++hashVal;
  71. hashVal %= arraySize;
  72. }
  73. return false;
  74. }
  75.  
  76.  
  77. public int size(){
  78. int size = 0;
  79. for(int j=0; j<arraySize; j++){
  80. if(hashArray[j] != null)
  81. size++;
  82. }
  83. return size;
  84. }
  85.  
  86.  
  87. public boolean isEmpty(){
  88. boolean isEmpty = true;
  89. for(int j=0; j<arraySize; j++){
  90. if(hashArray[j] != null){
  91. isEmpty = false;
  92. return isEmpty;
  93. }
  94. }
  95. return isEmpty;
  96. }
  97.  
  98.  
  99. public void resize(){
  100. Adresowanie_kwadratowe kw = new Adresowanie_kwadratowe(2*arraySize);
  101. for(int j=0; j<arraySize; j++){
  102. if(hashArray[j] != null)
  103. kw.put(j, hashArray[j]);
  104. }
  105. hashArray = kw.hashArray;
  106. arraySize = kw.hashArray.length;
  107. }
  108.  
  109.  
  110. public static void main(String[] args){
  111. Adresowanie_kwadratowe kw = new Adresowanie_kwadratowe(13);
  112. kw.dump();
  113. System.out.println("Czy tablica jest pusta? "+ kw.isEmpty());
  114. System.out.println("Dodano 3.");
  115. kw.put(3, new Obiekt(3));
  116. kw.dump();
  117. System.out.println("Dodano 5, 2, 1, 7, 20.");
  118. kw.put(5, new Obiekt(5));
  119. kw.put(2, new Obiekt(2));
  120. kw.put(1, new Obiekt(1));
  121. kw.put(7, new Obiekt(7));
  122. kw.put(20, new Obiekt(20));
  123. kw.dump();
  124. System.out.println("Dodano 35, 4, 8, 29, 31, 50, 69.");
  125. kw.put(35, new Obiekt(35));
  126. kw.put(4, new Obiekt(4));
  127. kw.put(8, new Obiekt(8));
  128. kw.put(29, new Obiekt(29));
  129. kw.put(31, new Obiekt(31));
  130. kw.put(50, new Obiekt(50));
  131. kw.put(69, new Obiekt(69));
  132. kw.dump();
  133. System.out.println("Czy tablica jest pusta? "+kw.isEmpty());
  134. System.out.println("Czy zawiera coś o kluczu 9? " + kw.containsKey(9));
  135. System.out.println("Czy zawiera coś o kluczu 1? " + kw.containsKey(1));
  136. System.out.println("Get 35? " + kw.get(35));
  137. }
  138.  
  139. }
Advertisement
Add Comment
Please, Sign In to add comment