The working code:
//import java.util.Hashtable;
public class HashingExample1 {
private int[] hashTable = new int[10];
public int hash(int ele){
return ele %
this.hashTable.length;
}
public boolean insert(int ele){
int pos = hash(ele);
System.out.println("Hashing key " +
pos);
if(this.hashTable[pos] == 0)
this.hashTable[pos] = ele;
else{
for(int i = pos
+ 1; i < this.hashTable.length; i++){
if(this.hashTable[i] == 0){
this.hashTable[i] =
ele;
return true;
}
}
}
return true;
}
public int search(int ele){
int loc = -1;
int pos = hash(ele);
if(hashTable[pos] == ele)
loc = pos;
else{
for(int i = pos
+ 1; i < this.hashTable.length; i++){
if(this.hashTable[i] == ele)
loc = i;
}
}
return loc;
}
public static void main(String[] args) {
HashingExample1 he = new
HashingExample1();
System.out.println(he.insert(245));
System.out.println(he.insert(342));
System.out.println(he.insert(156));
System.out.println(he.insert(345));
System.out.println(he.insert(999));
System.out.println(he.insert(109));
System.out.println("Element in hash
table");
for(int i = 0; i <
he.hashTable.length; i++)
System.out.printf("Hashtable[%d] ---> %d\n", i,
he.hashTable[i]);
int loc = he.search(342);
if(loc == -1)
System.out.println("Element 342 is not found");
else
System.out.println("Element 342 is found at index position " +
loc);
loc = he.search(345);
if(loc == -1)
System.out.println("Element 345 is not found");
else
System.out.println("Element 345 is found at index position " +
loc);
String str = "ABCDEF";
System.out.println(str.hashCode());
}
}
The Questions :
(DATA STRUCTURE BY JAVA)
If you have any problem with the code feel free to comment.
Program
import java.util.Scanner;
class HashingExample1 {
private int[] hashTable = new int[10];
public int hash(int ele) {
return ele %
this.hashTable.length;
}
public boolean insert(int ele) {
int pos = hash(ele);
System.out.println("Hashing key " +
pos);
if (this.hashTable[pos] == 0)
this.hashTable[pos] = ele;
else {
for (int i = pos
+ 1; i < this.hashTable.length; i++) {
if (this.hashTable[i] == 0) {
this.hashTable[i] =
ele;
return true;
}
}
}
return true;
}
public int search(int ele) {
int loc = -1;
int pos = hash(ele);
if (hashTable[pos] == ele)
loc = pos;
else {
for (int i = pos
+ 1; i < this.hashTable.length; i++) {
if (this.hashTable[i] == ele)
loc = i;
}
}
return loc;
}
//for deleting values
public void delete(int ele) {
int index = search(ele);
if (index == -1) {
System.out.println("The " + ele + " is not present in the Hash
Table");
return;
}
if (hashTable[index] == 0)
System.out.println("There is no element to delete");
else {
System.out.println("The " + ele + " is deleted");
hashTable[index]
= 0;
}
}
//for displaying hash table
public void displayHashTable() {
System.out.println("Element in hash
table");
for (int i = 0; i <
hashTable.length; i++)
System.out.printf("Hashtable[%d] ---> %d\n", i,
hashTable[i]);
}
}
public class Test{
public static void main(String[] args) {
Scanner sc = new Scanner
(System.in);
HashingExample1 he = new
HashingExample1();
//asking 5 values
System.out.println("Enter Five
values: ");
for(int i=1; i<=5; i++) {
System.out.print("Value "+i+": ");
he.insert(sc.nextInt());
}
System.out.println();
he.displayHashTable();
//asking values to be
searched
System.out.println();
System.out.print("Enter a value to
be serached: ");
int search = sc.nextInt();
System.out.println("Value is at
index: "+he.search(search));
he.displayHashTable();
//asking value to be deleted
System.out.println();
System.out.print("Enter a value to
be deleted: ");
int delete = sc.nextInt();
he.delete(delete);
he.displayHashTable();
sc.close();
}
}
Outputs
Get Answers For Free
Most questions answered within 1 hours.