Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

I need help creating code in java for a hash table using the requirements below.

ID: 3858329 • Letter: I

Question

I need help creating code in java for a hash table using the requirements below.

A Hash table pointing to a structure for a linked list that contains only the following information:

Each Hash Bucket Collision Item will have the following Information:

ID: Integer; //identifier key for future needs

Hash Bucket Functions/Methods:

Input constructor: //to accept a string for the name and additional information for each contributor (you will only need the ID portion of the input data)

Hash Function constructor: (Hint: You only have 5 Hash buckets, so the function can be a very simple calculation.)

Pop constructor

Push constructor

Print constructor: //to show the contents of a Hash bucket

Explanation / Answer

import java.util.Scanner;

/* Class HashTableTest */
public class HashTableTest
{
public static void main(String[] args)
{
Scanner scan = new Scanner(System.in);
System.out.println("Hash Table Test ");
System.out.println("Enter size");
/* Make object of HashTableChainingSinglyLinkedList */
HashTableImpl htcsll = new HashTableImpl(scan.nextInt() );

char ch;
/* Perform HashTable operations */
do
{
System.out.println(" Hash Table ");
System.out.println("1. push ");
System.out.println("2. pop");
System.out.println("3. clear");
System.out.println("4. check size");
System.out.println("5. check empty");

int choice = scan.nextInt();
switch (choice)
{
case 1 :
System.out.println("Enter integer element to insert");
htcsll.push( scan.nextInt() );
break;
case 2 :   
System.out.println("Enter integer element to delete");
htcsll.pop( scan.nextInt() );
break;
case 3 :
htcsll.makeEmpty();
System.out.println("Hash Table Cleared ");
break;
case 4 :
System.out.println("Size = "+ htcsll.getSize() );
break;
case 5 :
System.out.println("Empty status = "+ htcsll.isEmpty() );
break;
default :
System.out.println("Wrong Entry ");
break;   
}
/* Display hash table */
htcsll.printHash();

System.out.println(" Do you want to continue (Type y or n) ");
ch = scan.next().charAt(0);
} while (ch == 'Y'|| ch == 'y');
}
}

import java.util.Scanner;

/* Node for singly linked list */
class HNode
{
HNode next;
int data;

/* Constructor */
public HNode(int x)
{
data = x;
next = null;
}
}

/* Class HashTableChainingSinglyLinkedList */
class HashTableImpl
{
private HNode[] table;
private int size ;

/* Constructor */
public HashTableImpl(int tableSize)
{
table = new HNode[ nextPrime(tableSize) ];
size = 0;
}
/* check if hash table is empty */
public boolean isEmpty()
{
return size == 0;
}
/* Function to clear hash table */
public void makeEmpty()
{
int l = table.length;
table = new HNode[l];
size = 0;
}
/* Get Size of Hash Table */
public int getSize()
{
return size;
}
/* push an element */
public void push(int val)
{
size++;
int pos = myhash(val);
HNode nptr = new HNode(val);
if (table[pos] == null)
table[pos] = nptr;
else
{
nptr.next = table[pos];
table[pos] = nptr;
}
}
/* pop an element */
public void pop(int val)
{
int pos = myhash(val);
HNode start = table[pos];
HNode end = start;
if (start.data == val)
{
size--;
table[pos] = start.next;
return;
}
while (end.next != null && end.next.data != val)
end = end.next;
if (end.next == null)
{
System.out.println(" Element not found ");
return;
}
size--;
if (end.next.next == null)
{
end.next = null;
return;
}
end.next = end.next.next;
table[pos] = start;
}
/* hashing logic */
private int myhash(Integer x )
{
int hashVal = x.hashCode( );
hashVal %= table.length;
if (hashVal < 0)
hashVal += table.length;
return hashVal;
}
/*generate next prime number >= n */
private static int nextPrime( int n )
{
if (n % 2 == 0)
n++;
for ( ; !isPrime( n ); n += 2);

return n;
}
/*check if given number is prime */
private static boolean isPrime( int n )
{
if (n == 2 || n == 3)
return true;
if (n == 1 || n % 2 == 0)
return false;
for (int i = 3; i * i <= n; i += 2)
if (n % i == 0)
return false;
return true;
}
public void printHash ()
{
System.out.println();
for (int i = 0; i < table.length; i++)
{
System.out.print ("Bucket " + i + ": ");   
HNode start = table[i];
while(start != null)
{
System.out.print(start.data +" ");
start = start.next;
}
System.out.println();
}
}   
}

Sample Output:

Hash Table Test


Enter size
5

Hash Table

1. push
2. pop
3. clear
4. check size
5. check empty
1
Enter integer element to insert
12

Bucket 0:
Bucket 1:
Bucket 2: 12
Bucket 3:
Bucket 4:

Do you want to continue (Type y or n)

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote