Preview:
import java.util.*;

class LinearProb {
    int csize, maxsize;
    String keys[];
    String vals[];

    LinearProb(int capacity) {
        csize = 0;
        maxsize = capacity;
        keys = new String[maxsize];
        vals = new String[maxsize];
    }

    public int hash(String key) {
        return Math.abs(key.hashCode()) % maxsize; // Corrected hash function
    }

    public void insert(String key, String val) {
        int index = hash(key);
        while (keys[index] != null && !keys[index].equals(key)) {
            index = (index + 1) % maxsize; // Linear probing
        }
        keys[index] = key;
        vals[index] = val;
        csize++;
    }

    public void print() {
        System.out.println("Hash Table");
        for (int i = 0; i < maxsize; i++) {
            if (keys[i] != null) {
                System.out.println(keys[i] + " " + vals[i]);
            }
        }
    }
}

public class Main {
    public static void main(String args[]) {
        Scanner s = new Scanner(System.in);
        System.out.println("Enter size");
        int capacity = s.nextInt();
        LinearProb lp = new LinearProb(capacity);
        for (int i = 0; i < capacity; i++) {
            System.out.println("Enter key and val");
            lp.insert(s.next(), s.next());
        }
        lp.print();
    }
}


downloadDownload PNG downloadDownload JPEG downloadDownload SVG

Tip: You can change the style, width & colours of the snippet with the inspect tool before clicking Download!

Click to optimize width for Twitter