import java.util.*;
class LinearProbing {
int[] freq, status, hashTable;
int capacity;
int EMPTY = 0, DELETED = -1, OCCUPIED = 1;
LinearProbing(int capacity) {
this.capacity = capacity;
freq = new int[capacity];
status = new int[capacity];
hashTable = new int[capacity];
}
public int hashFunction(int key) {
return key % capacity;
}
public boolean search(int key) {
int index = hashFunction(key);
int start = index;
while (status[index] != EMPTY) {
if (hashTable[index] == key && status[index] == OCCUPIED)
return true;
index = (index + 1) % capacity;
if (index == start) break;
}
return false;
}
public void insert(int key) {
int index = hashFunction(key);
int start = index;
while (status[index] != EMPTY) {
if (hashTable[index] == key) {
freq[index]++;
return;
}
index = (index + 1) % capacity;
if (index == start) return;
}
hashTable[index] = key;
status[index] = OCCUPIED;
freq[index] = 1;
}
public boolean delete(int key) {
int index = hashFunction(key);
int start = index;
while (status[index] != EMPTY) {
if (hashTable[index] == key && status[index] == OCCUPIED) {
freq[index]--;
if (freq[index] <= 0) status[index] = DELETED;
return true;
}
index = (index + 1) % capacity;
if (index == start) break;
}
return false;
}
public void display() {
for (int i = 0; i < capacity; i++) {
if (status[i] == OCCUPIED)
System.
out.
println(i
+ " -> " + hashTable
[i
] + " : " + freq
[i
]); else
System.
out.
println(i
+ " -> - : -"); }
}
}
public class Main {
public static void main
(String[] args
) { LinearProbing map = new LinearProbing(10);
map.insert(45);
map.insert(22);
map.insert(35);
map.insert(67);
map.insert(78);
map.insert(15);
map.insert(67);
map.insert(67);
map.insert(90);
map.insert(90);
System.
out.
println(map.
delete(15)); System.
out.
println(map.
search(99)); map.display();
}
}