Skip to content

Commit

Permalink
Implement a hash table
Browse files Browse the repository at this point in the history
  • Loading branch information
sangaryousmane committed Mar 19, 2024
1 parent f87c5b4 commit 485031f
Show file tree
Hide file tree
Showing 2 changed files with 92 additions and 18 deletions.
22 changes: 4 additions & 18 deletions src/advance/crackingCodingInterview/Tests.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,28 +5,14 @@
import advance.crackingCodingInterview.linkedList.general.KthToLast;
import advance.crackingCodingInterview.linkedList.general.PartitionAroundX;
import advance.crackingCodingInterview.linkedList.palindromes.PalindromeStack;
import advance.crackingCodingInterview.oops.Hasher;

public class Tests {


public static void main(String[] args) {
// Create a linked list: 1 -> 3 -> 5 -> 7 -> 9 -> 11 -> 13
ListNode head = new ListNode(1);
PartitionAroundX kthToLast = new PartitionAroundX();
kthToLast.add(3);
kthToLast.add(5);
kthToLast.add(8);
kthToLast.add(5);
kthToLast.add(10);
kthToLast.add(2);
kthToLast.add(1);

// Test for 2nd to last
System.out.println(kthToLast.partition(head, 5));
kthToLast.print();

// Test for 4th to last
// ListNode result2 = kthToLast.kthToLast(head, 6);
// System.out.println("6th to last: " + result2.data);
Hasher<Integer, String> hash=new Hasher<>(3);
hash.put(1, "Deleted");
System.out.println(hash.get(1));
}
}
88 changes: 88 additions & 0 deletions src/advance/crackingCodingInterview/oops/Hasher.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
package advance.crackingCodingInterview.oops;

import java.util.ArrayList;
import java.util.List;

/**
* Design and implement a hash table which
* uses chaining(linked list) to handle collision.
*/
public class Hasher<K, V> {


// A private static class implemented as a doubly linked list
private static class LinkedlistNode<K, V>{
private LinkedlistNode<K, V> prev;
private LinkedlistNode<K, V> next;
private K key;
private V value;

public LinkedlistNode(K key, V value){
this.key = key;
this.value = value;
}
}

private ArrayList<LinkedlistNode<K, V>> arr;

public Hasher(int capacity) {
this.arr = new ArrayList<>();
arr.ensureCapacity(capacity);

for(int i = 0; i < capacity; i++){
arr.add(null);
}
}

// Inset key and value into hash table
public void put(K key, V value){
LinkedlistNode<K, V> node = getNodeForKey(key);
if (node != null){
node.value = value;
return;
}

node = new LinkedlistNode<K, V>(key, value);
int index = getIndexForKey(key);
if(arr.get(index) != null){
node.next = arr.get(index);
node.next.prev = node;
}
arr.set(index, node);
}

// Get value for key
public V get(K key){
LinkedlistNode<K, V> node = getNodeForKey(key);
return node == null ? null : node.value;
}

public void remove(K key){
LinkedlistNode<K, V> node = getNodeForKey(key);
if (node == null) return;

if (node.prev != null){
node.prev.next = node.next;
} else{
int hashKey = getIndexForKey(key);
arr.set(hashKey, node.next);
}
if (node.next != null)
node.next.prev = node.prev;
}


// Get linkedlist node associated with a given node
private LinkedlistNode<K, V> getNodeForKey(K key){
int index = getIndexForKey(key);
LinkedlistNode<K, V> current = arr.get(index);
while (current != null){
if(current.key == key) return current;
current = current.next;
}
return null;
}
private int getIndexForKey(K key) {
return Math.abs(key.hashCode() % arr.size());
}
}

0 comments on commit 485031f

Please sign in to comment.