Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package lab10_final;
- public class Adresowanie_kwadratowe {
- private Obiekt[] hashArray;
- private int arraySize;
- public Adresowanie_kwadratowe(int size){
- arraySize = size;
- hashArray = new Obiekt[arraySize];
- }
- public void dump(){
- System.out.print("Tablica: ");
- for(int j=0; j<arraySize; j++){
- if(hashArray[j] != null)
- System.out.print(hashArray[j].getValue()+" ");
- else
- System.out.print("* ");
- }
- System.out.println();
- }
- public int hashFunction(int key, int i){
- int result = key % arraySize + i*i % arraySize;
- return result ;
- }
- public void put(Obiekt ob){
- /* for(int i =0; i<hashArray.length;i++){
- if()
- }*/
- if((double)size()/(double)arraySize < 0.75){
- int value = ob.getValue();
- int key = ob.getKey();
- int i = 0;
- int hashVal = hashFunction(key, i);
- while(hashArray[hashVal] != null){
- i++;
- hashVal = hashFunction(key, i);
- }
- hashArray[hashVal] = ob;
- }else{
- resize();
- put(ob);
- }
- }
- public Obiekt get(int key){
- int i =0;
- int hashVal = hashFunction(key, i);
- while(hashArray[hashVal] != null && hashArray[hashVal].getKey() != key){
- i++;
- hashVal = hashFunction(key, i);
- }
- if (hashArray[hashVal] == null)
- return null;
- else
- return hashArray[hashVal];
- }
- /* int hashVal = hashFunction(key);
- while(hashArray[hashVal] != null){
- if(hashArray[hashVal].getValue() == key)
- return hashArray[hashVal];
- ++hashVal;
- hashVal %= arraySize;
- }
- return null;
- }
- */
- public boolean containsKey(int key){
- for(int i =0; i<hashArray.length;i++){
- if(hashArray[i] != null){
- if(hashArray[i].getKey() == key)
- return true;
- }
- }
- return false;
- }
- /* int hashVal = hashFunction(key);
- while(hashArray[hashVal] != null){
- if(hashArray[hashVal].getValue() == key)
- return true;
- ++hashVal;
- hashVal %= arraySize;
- }
- return false;
- }
- */
- public int size(){
- int size = 0;
- for(int j=0; j<arraySize; j++){
- if(hashArray[j] != null)
- size++;
- }
- return size;
- }
- public boolean isEmpty(){
- boolean isEmpty = true;
- for(int j=0; j<arraySize; j++){
- if(hashArray[j] != null){
- isEmpty = false;
- return isEmpty;
- }
- }
- return isEmpty;
- }
- public void resize(){
- Adresowanie_kwadratowe kw = new Adresowanie_kwadratowe(2*arraySize);
- for(int j=0; j<arraySize; j++){
- if(hashArray[j] != null)
- kw.put(hashArray[j]);
- }
- hashArray = kw.hashArray;
- arraySize = kw.hashArray.length;
- }
- public static void main(String[] args){
- Adresowanie_kwadratowe kw = new Adresowanie_kwadratowe(13);
- kw.dump();
- System.out.println("Czy tablica jest pusta? "+ kw.isEmpty());
- System.out.println("Dodano 3.");
- kw.put(new Obiekt(3,3));
- kw.dump();
- System.out.println("Dodano 5, 2, 1, 7, 20.");
- kw.put(new Obiekt(5,5));
- kw.put(new Obiekt(2,2));
- kw.put(new Obiekt(1,1));
- kw.put(new Obiekt(7,7));
- kw.put(new Obiekt(20,20));
- kw.dump();
- System.out.println("Dodano 35, 4, 8, 29, 31, 50, 69.");
- kw.put(new Obiekt(35,35));
- kw.put(new Obiekt(4,4));
- kw.put(new Obiekt(8,8));
- kw.put(new Obiekt(29,29));
- kw.put(new Obiekt(31,31));
- kw.put(new Obiekt(50,50));
- kw.put(new Obiekt(69,69));
- kw.dump();
- System.out.println("Czy tablica jest pusta? "+kw.isEmpty());
- System.out.println("Czy zawiera coś o kluczu 9? " + kw.containsKey(9));
- System.out.println("Czy zawiera coś o kluczu 1? " + kw.containsKey(1));
- System.out.println("Get 35? " + kw.get(35));
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment