-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHashTableVAC.java
More file actions
59 lines (52 loc) · 1.64 KB
/
HashTableVAC.java
File metadata and controls
59 lines (52 loc) · 1.64 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package datastructure;
/**
*
* @author Vladimir Aca
*/
public class HashTableVAC<T> {
int size;
NodeHash[] arrayHash;
public HashTableVAC(int size) {
this.size = size;
this.arrayHash = new NodeHash[size];
for(int k=0; k<size; k++){
this.arrayHash[k] = new NodeHash();
}
}
public Integer getHash(int key){
int hashKey = key%this.size;
return hashKey;
}
public void put(int key, Object value){
NodeHash newNode = new NodeHash(null, value, key);
int hashNodeKey = this.getHash(key);
NodeHash currentHashList = this.arrayHash[hashNodeKey];
newNode.next = currentHashList.next;
currentHashList.next = newNode;
/*if(currentHashList == null){
currentHashList = newNode;
}else{
newNode.next = currentHashList;
currentHashList = newNode;
}*/
//this.arrayHash[hashNodeKey] = currentHashList;
}
public T getElement(Integer key){
int hashKey = this.getHash(key);
T currentValue = null;
NodeHash currentHashList = this.arrayHash[hashKey];
NodeHash currentNodeHash = currentHashList;
while(currentNodeHash != null){
if(currentNodeHash.getKey() == key){
return (T) currentNodeHash.getValue();
}
currentNodeHash = currentNodeHash.next;
}
return currentValue;
}
}