In JAVA I am using Data Structure and algorithms in Java Second Edition Program
ID: 3701741 • Letter: I
Question
In JAVA
I am using Data Structure and algorithms in Java Second Edition
Program Must Compile and run Please WRITE UNDER ONE FILE!
Implement a linear probe hash table that stores strings. You’ll need a hash
function that converts a string to an index number; see the section “Hashing
Strings” in this chapter. Assume the strings will be lowercase words, so 26
characters will suffice.
MUST COMPILE AND RUN ,PLEASE DISPLAY OUTPUT, PLEASE Write under one File
Please include main method below with program >>
// 1. In time of insertion every no low case characters should be substituted by ‘z’.
// 2. Not existing “null” is displayed as “#Null#”.
// 3. Deleted item is represented as “qzqzq” and displayed as “Del#”
>>public static void main(String[] args) throws IOException
{
String aString = new String();
StringItem stringKey, aStringItem;
int size, n, keysPerCell;
// get sizes
System.out.print("Enter size of hash table: ");
size = getInt();
keysPerCell = 10;
// make table
HashTable theHashTable = new HashTable(size);
while(true) // interact with user
{
System.out.print("Enter first letter of ");
System.out.print("show, insert, delete, or find: ");
char choice = getChar();
switch(choice)
{
case 's':
theHashTable.displayTable();
break;
case 'i':
System.out.print("Enter word to insert: ");
aString = getString();
aStringItem = new StringItem(aString);
// could add other data items here
theHashTable.insert(aStringItem);
break;
case 'd':
System.out.print("Enter string to delete: ");
aString = getString();
if( theHashTable.delete(aString) == null)
System.out.println("Can't find " + aString);
else
System.out.println("Deleted " + aString);
break;
case 'f':
System.out.print("Enter string to find: ");
aString = getString();
aStringItem = theHashTable.find(aString);
if(aStringItem != null)
{
System.out.print("Found ");
aStringItem.displayItem();
System.out.println("");
}
else
System.out.println("Can't find " + aString);
break;
default:
System.out.print("Invalid entry ");
} // end switch
} // end while
} // end main()
The output may look like:
Enter size of hash table: 11
Enter first letter of show, insert, delete, or find: s
Table: #Null# #Null# #Null# #Null# #Null# #Null# #Null# #Null# #Null# #Null# #Null#
Enter first letter of show, insert, delete, or find: i
Enter word to insert: one
Enter first letter of show, insert, delete, or find: d
Enter string to delete: one
Deleted one
Enter first letter of show, insert, delete, or find: s
Table: #Null# #Null# #Null# #Null# #Del# #Null# #Null# #Null# #Null# #Null# #Null#
Enter first letter of show, insert, delete, or find: i
Enter word to insert: one
Enter first letter of show, insert, delete, or find: s
Table: #Null# #Null# #Null# #Null# #Del# one #Null# #Null# #Null# #Null# #Null#
Enter first letter of show, insert, delete, or find: i
Enter word to insert: two three
Enter first letter of show, insert, delete, or find: s
Table: #Null# twozthree #Null# #Null# #Del# one #Null# #Null# #Null# #Null# #Null#
Enter first letter of show, insert, delete, or find:
Explanation / Answer
package resouces;
import java.io.IOException;
import java.util.Hashtable;
import java.util.Map.Entry;
import java.util.Scanner;
public class Hashing {
public static void main(String[] args) throws IOException
{
String aString = new String();
String stringKey=null, aStringItem=null;
int size, n=1001, keysPerCell;
Scanner scn=new Scanner(System.in);
// get sizes
System.out.print("Enter size of hash table: ");
size = scn.nextInt();
keysPerCell = 10;
// make table
Hashtable<String,String> theHashTable = new Hashtable<String,String>(size);
while(true) // interact with user
{
System.out.print("Enter first letter of ");
System.out.print("show, insert, delete, or find: ");
char choice = scn.next().charAt(0);
switch(choice)
{
case 's':
if(theHashTable==null) {
System.out.println("Empty hash table, please insert first:");
break;
}
for(Entry<String, String> m:theHashTable.entrySet()){
System.out.println(m.getKey()+" "+m.getValue());
}
break;
case 'i':
System.out.print("Enter word to insert: ");
aString = scn.next();
String aStringKey=n+"";
aStringItem = new String(aString);
// could add other data items here
theHashTable.put(aStringKey,aStringItem);
n++;
break;
case 'd':
System.out.print("Enter string to delete: ");
aString =scn.nextLine();
if( theHashTable.remove(stringKey, aString)== false)
System.out.println("Can't find " + aString);
else
System.out.println("Deleted " + aString);
break;
case 'f':
System.out.print("Enter string to find: ");
aString = scn.next();
aStringItem = theHashTable.get(aString);
if(aStringItem != null)
{
for(Entry<String, String> m:theHashTable.entrySet()){
System.out.println(m.getKey()+" "+m.getValue());
if(aStringItem.equals(m.getValue()))
System.out.print("Found : "+aStringItem);
}
}
else{
System.out.println("Can't find " + aString);
}
break;
default: System.out.print("Invalid entry ");
} // end switch
} // end while
} // end main()
}
/*output-
Enter size of hash table: 2
Enter first letter of show, insert, delete, or find: i
Enter word to insert: taeha
Enter first letter of show, insert, delete, or find: i
Enter word to insert: tejaa
Enter first letter of show, insert, delete, or find: s
1001 taeha
1002 tejaa
*/
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.