Pabl0o0

Untitled

May 24th, 2017
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.73 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, int i){
  29. int result = key % arraySize + i*i % arraySize;
  30. return result ;
  31. }
  32.  
  33.  
  34. public void put(Obiekt ob){
  35. /* for(int i =0; i<hashArray.length;i++){
  36. if()
  37. }*/
  38. if((double)size()/(double)arraySize < 0.75){
  39. int value = ob.getValue();
  40. int key = ob.getKey();
  41. int i = 0;
  42. int hashVal = hashFunction(key, i);
  43.  
  44. while(hashArray[hashVal] != null){
  45. i++;
  46. hashVal = hashFunction(key, i);
  47. }
  48. hashArray[hashVal] = ob;
  49. }else{
  50. resize();
  51. put(ob);
  52. }
  53. }
  54.  
  55.  
  56.  
  57. public Obiekt get(int key){
  58. int i =0;
  59. int hashVal = hashFunction(key, i);
  60. while(hashArray[hashVal] != null && hashArray[hashVal].getKey() != key){
  61. i++;
  62. hashVal = hashFunction(key, i);
  63. }
  64. if (hashArray[hashVal] == null)
  65. return null;
  66. else
  67. return hashArray[hashVal];
  68.  
  69.  
  70. }
  71. /* int hashVal = hashFunction(key);
  72.  
  73. while(hashArray[hashVal] != null){
  74. if(hashArray[hashVal].getValue() == key)
  75. return hashArray[hashVal];
  76. ++hashVal;
  77. hashVal %= arraySize;
  78. }
  79. return null;
  80. }
  81. */
  82.  
  83. public boolean containsKey(int key){
  84.  
  85. for(int i =0; i<hashArray.length;i++){
  86. if(hashArray[i] != null){
  87. if(hashArray[i].getKey() == key)
  88. return true;
  89. }
  90. }
  91. return false;
  92.  
  93. }
  94. /* int hashVal = hashFunction(key);
  95.  
  96. while(hashArray[hashVal] != null){
  97. if(hashArray[hashVal].getValue() == key)
  98. return true;
  99. ++hashVal;
  100. hashVal %= arraySize;
  101. }
  102. return false;
  103. }
  104. */
  105.  
  106.  
  107. public int size(){
  108. int size = 0;
  109. for(int j=0; j<arraySize; j++){
  110. if(hashArray[j] != null)
  111. size++;
  112. }
  113. return size;
  114. }
  115.  
  116.  
  117. public boolean isEmpty(){
  118. boolean isEmpty = true;
  119. for(int j=0; j<arraySize; j++){
  120. if(hashArray[j] != null){
  121. isEmpty = false;
  122. return isEmpty;
  123. }
  124. }
  125. return isEmpty;
  126. }
  127.  
  128.  
  129. public void resize(){
  130. Adresowanie_kwadratowe kw = new Adresowanie_kwadratowe(2*arraySize);
  131. for(int j=0; j<arraySize; j++){
  132. if(hashArray[j] != null)
  133. kw.put(hashArray[j]);
  134. }
  135. hashArray = kw.hashArray;
  136. arraySize = kw.hashArray.length;
  137. }
  138.  
  139.  
  140. public static void main(String[] args){
  141. Adresowanie_kwadratowe kw = new Adresowanie_kwadratowe(13);
  142. kw.dump();
  143. System.out.println("Czy tablica jest pusta? "+ kw.isEmpty());
  144. System.out.println("Dodano 3.");
  145. kw.put(new Obiekt(3,3));
  146. kw.dump();
  147. System.out.println("Dodano 5, 2, 1, 7, 20.");
  148. kw.put(new Obiekt(5,5));
  149. kw.put(new Obiekt(2,2));
  150. kw.put(new Obiekt(1,1));
  151. kw.put(new Obiekt(7,7));
  152. kw.put(new Obiekt(20,20));
  153. kw.dump();
  154. System.out.println("Dodano 35, 4, 8, 29, 31, 50, 69.");
  155. kw.put(new Obiekt(35,35));
  156. kw.put(new Obiekt(4,4));
  157. kw.put(new Obiekt(8,8));
  158. kw.put(new Obiekt(29,29));
  159. kw.put(new Obiekt(31,31));
  160. kw.put(new Obiekt(50,50));
  161. kw.put(new Obiekt(69,69));
  162. kw.dump();
  163. System.out.println("Czy tablica jest pusta? "+kw.isEmpty());
  164. System.out.println("Czy zawiera coś o kluczu 9? " + kw.containsKey(9));
  165. System.out.println("Czy zawiera coś o kluczu 1? " + kw.containsKey(1));
  166. System.out.println("Get 35? " + kw.get(35));
  167. }
  168.  
  169. }
Advertisement
Add Comment
Please, Sign In to add comment