public class HashLinear { public static int empty = -Integer.MAX_VALUE; public int[] table = {empty, empty, empty, empty, empty, empty, empty, empty, empty, empty, empty, empty, empty}; public int hash(int x) { return (3*x+5)%table.length; } public void insert(int x) { if (table[hash(x)] == empty) { table[hash(x)] = x; } else { for (int i = 1; i < table.length; i++) { if (table[(hash(x)+i)%table.length] == empty) { table[(hash(x)+i)%table.length] = x; break; } } } } public boolean contains(int x) { if (table[hash(x)] == x) { return true; } else { for (int i = 1; i < table.length; i++) { if (table[(hash(x)+i)%table.length] == x) { return true; } } } return false; } public static void main(String[] args) { HashLinear HL = new HashLinear(); HL.insert(5); HL.insert(1); HL.insert(19); HL.insert(23); HL.insert(14); HL.insert(17); HL.insert(32); HL.insert(30); HL.insert(2); HL.insert(7); HL.insert(10); for (int i = 0; i < HL.table.length; i++) System.out.println(i + ":" + ((HL.table[i] != empty) ? HL.table[i] : "")); } }