Question

The working code: //import java.util.Hashtable; public class HashingExample1 {    private int[] hashTable = new int[10];...

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 :

  • Ask user to insert 5 different values in a hash table.
  • Ask user to enter key value and search about and display the result.
  • Allow user to delete key send to delete method and display the result.

(DATA STRUCTURE BY JAVA)

Homework Answers

Answer #1

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

Know the answer?
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for?
Ask your own homework help question
Similar Questions
please fix code to delete by studentname import java.util.Scanner; public class COurseCom666 {     private String...
please fix code to delete by studentname import java.util.Scanner; public class COurseCom666 {     private String courseName;     private String[] students = new String[1];     private int numberOfStudents;     public COurseCom666(String courseName) {         this.courseName = courseName;     }     public String[] getStudents() {         return students;     }     public int getNumberOfStudents() {         return numberOfStudents;     }     public void addStudent(String student) {         if (numberOfStudents == students.length) {             String[] a = new String[students.length + 1];            ...
do a code trace on how a key gets deleted package interview; import java.util.*; public class...
do a code trace on how a key gets deleted package interview; import java.util.*; public class binarySearchTree { Node root; void insert(int key) { root = insertRec(root,key); } void delete(int key) { root = deleteRec(root,key); } Node insertRec(Node root, int key) { if(root == null) { root = new Node(key); return root; } if(key < root.key) { root.leftChild = insertRec(root.leftChild,key); } else if(key > root.key){ root.rightChild = insertRec(root.rightChild,key); } return root; } Node deleteRec(Node root, int key) { if(root ==...
Please explain code 1 and code 2 for each lines code 1 public class MyQueue {...
Please explain code 1 and code 2 for each lines code 1 public class MyQueue {    public static final int DEFAULT_SIZE = 10;    private Object data[];    private int index; code 2 package test; import java.util.*; /* Class Node */ class Node { protected Object data; protected Node link; /* Constructor */ public Node() { link = null; data = 0; } /* Constructor */ public Node(Object d,Node n) { data = d; link = n; } /*...
import java.io.PrintStream; import java.util.Arrays; public class joker { public static int smallest(int[] v1, int[] v2) {...
import java.io.PrintStream; import java.util.Arrays; public class joker { public static int smallest(int[] v1, int[] v2) { return 0; } public static int[] convert1(String str) { return new int[1]; } public static String convert2(int[] v) { return ""; } public static void main(String[] args) { testSmallest(); testConvert(); } public static void testSmallest() { System.out.println("Testing your method smallest(...)"); int[][] testVectors1 = new int[][]{{1, 2, 3}, {1, 2, 3, 4}, {1, 2, 3}, {1, 2, 3}, {2, 3, 4}}; int[][] testVectors2 = new...
import java.util.Scanner; public class FindMinLength { public static int minLength(String[] array) { int minLength = array[0].length();...
import java.util.Scanner; public class FindMinLength { public static int minLength(String[] array) { int minLength = array[0].length(); for (int i = 0; i < array.length; i++) { if (array[i].length() < minLength) minLength = array[i].length(); } return minLength; } public static void main(String[] args) { Scanner in = new Scanner(System.in); String[] strings = new String[50]; for (int i = 0; i < strings.length; i++) { System.out.print("Enter string " + (i + 1) + ": "); strings[i] = in.nextLine(); } System.out.println("Length of smallest...
How do I implement this method BalancedByNodeCount() ? public class BinarySearchTree { private Node root; private...
How do I implement this method BalancedByNodeCount() ? public class BinarySearchTree { private Node root; private boolean isBalancedByNodeCount() { /**************************************************************************** Implement this method and replace the return statement below with your code. * Definition of Balance tree (On page 372 of book): * An unbalanced tree is created when most of the nodes are on one side of the root or the other. ****************************************************************************/    return false; }       public static void main(String args[]) { int[] values1 = {50,...
Starting with my code for Hash Table example (hash.cpp) Write your own main and insert function...
Starting with my code for Hash Table example (hash.cpp) Write your own main and insert function in C++ to insert the sequence of integers { 121, 81, 16, 100, 25, 0, 1, 9, 4, 36, 64, 49,) using a table of size 17. Implement your own rehashing algorithm of choice and run the same sequence of input using a table of size 7. Code for hash.cpp: const int TABLE_SIZE = 128; class HashMap { private: HashEntry **table; public: HashMap() {...
import java.util.Scanner; import java.util.Random; public class DiceRoll {    public static final int SIDES = 6;...
import java.util.Scanner; import java.util.Random; public class DiceRoll {    public static final int SIDES = 6;    public static void main(String[] args) {        // TODO Auto-generated method stub        System.out.println("Enter the number of times a 6 sided die should be rolled ");        Scanner keyboard = new Scanner(System.in);        Random r = new Random();               int times = keyboard.nextInt();        boolean valid = false;        while(!valid) {           ...
import java.util.Scanner; public class InRange { private int min; private int max; public InRange(int initialMin, int...
import java.util.Scanner; public class InRange { private int min; private int max; public InRange(int initialMin, int initialMax) { min = initialMin; max = initialMax; } // You need to write two instance methods: // 1.) A method named inRange, which takes an int. // This returns true if the int is within the given range // (inclusive), else false. // // 2.) A method named outOfRange which takes an int. // This returns false if the int is within the...
in java need uml diagram import java.util.ArrayList; import java.util.*; public class TodoList { String date=""; String...
in java need uml diagram import java.util.ArrayList; import java.util.*; public class TodoList { String date=""; String work=""; boolean completed=false; boolean important=false; public TodoList(String a,String b,boolean c,boolean d){ this.date=a; this.work=b; this.completed=c; this.important=d; } public boolean isCompleted(){ return this.completed; } public boolean isImportant(){ return this.important; } public String getDate(){ return this.date; } public String getTask(){ return this.work; } } class Main{ public static void main(String[] args) { ArrayList<TodoList> t1=new ArrayList<TodoList>(); TodoList t2=null; Scanner s=new Scanner(System.in); int a; String b="",c=""; boolean d,e; char...
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT