|
| 1 | +package Hashtable; |
| 2 | +import java.io.*; |
| 3 | + |
| 4 | +class DataItem |
| 5 | +{ |
| 6 | + private int iData; |
| 7 | +//----------------------------------------------------------------------------------// |
| 8 | + public DataItem(int ii) |
| 9 | + { |
| 10 | + iData = ii; |
| 11 | + } |
| 12 | +//----------------------------------------------------------------------------------// |
| 13 | + public int getKey() |
| 14 | + { |
| 15 | + return iData; |
| 16 | + } |
| 17 | +} |
| 18 | +//----------------------------------------------------------------------------------// |
| 19 | +class HashTable |
| 20 | +{ |
| 21 | + private DataItem[] hashArray; |
| 22 | + private int arraySize; |
| 23 | + private DataItem nonitem; |
| 24 | +//----------------------------------------------------------------------------------// |
| 25 | + public HashTable(int size) |
| 26 | + { |
| 27 | + arraySize = size; |
| 28 | + hashArray = new DataItem[arraySize]; |
| 29 | + nonitem = new DataItem(-1); |
| 30 | + } |
| 31 | +//----------------------------------------------------------------------------------// |
| 32 | + public void displayTable() |
| 33 | + { |
| 34 | + System.out.print("Table: "); |
| 35 | + for(int j = 0; j < arraySize; j++) |
| 36 | + { |
| 37 | + if(hashArray[j] != null) |
| 38 | + System.out.print(hashArray[j].getKey() + " "); |
| 39 | + else |
| 40 | + System.out.print("** "); |
| 41 | + } |
| 42 | + System.out.println(""); |
| 43 | + } |
| 44 | +//----------------------------------------------------------------------------------// |
| 45 | + public int hashFunc1(int key) |
| 46 | + { |
| 47 | + return key % arraySize; |
| 48 | + } |
| 49 | +//----------------------------------------------------------------------------------// |
| 50 | + public int hashFunc2(int key) |
| 51 | + { |
| 52 | + return 5 - key % 5; //non-zero, less than array size, different from hF1 (array size must be relatively prime to 5, 4, 3, and 2) |
| 53 | + } |
| 54 | +//----------------------------------------------------------------------------------// |
| 55 | + public void insert(int key, DataItem item) //assumes table is not full |
| 56 | + { |
| 57 | + int hashVal = hashFunc1(key); // hash the key |
| 58 | + int stepSize = hashFunc2(key);// get step siz until empty cell or -1 |
| 59 | + |
| 60 | + while(hashArray[hashVal] != null && hashArray[hashVal].getKey() != -1) |
| 61 | + { |
| 62 | + hashVal += stepSize; //add the step |
| 63 | + hashVal %= arraySize;//for the wrap around |
| 64 | + } |
| 65 | + hashArray[hashVal] = item; //insert item |
| 66 | + } |
| 67 | +//----------------------------------------------------------------------------------// |
| 68 | + public DataItem delete(int key) |
| 69 | + { |
| 70 | + int hashVal = hashFunc1(key); |
| 71 | + int stepSize = hashFunc2(key); |
| 72 | + |
| 73 | + while(hashArray[hashVal] != null)//until empty cell, is correct hashVal? |
| 74 | + { |
| 75 | + if(hashArray[hashVal].getKey() == key) |
| 76 | + { |
| 77 | + DataItem temp = hashArray[hashVal]; //save item |
| 78 | + hashArray[hashVal] = nonitem; // delete item |
| 79 | + return temp; //return item |
| 80 | + } |
| 81 | + hashVal += stepSize; |
| 82 | + hashVal %= arraySize; |
| 83 | + } |
| 84 | + return null; |
| 85 | + } |
| 86 | +//----------------------------------------------------------------------------------// |
| 87 | + public DataItem find(int key) //assumes table is not full **find item key** |
| 88 | + { |
| 89 | + int hashVal = hashFunc1(key); |
| 90 | + int stepSize = hashFunc2(key); |
| 91 | + |
| 92 | + while(hashArray[hashVal] != null) |
| 93 | + { |
| 94 | + if(hashArray[hashVal].getKey() == key) // found? |
| 95 | + return hashArray[hashVal]; // return item |
| 96 | + hashVal += stepSize; //add the step |
| 97 | + hashVal %= arraySize; //for wraparound |
| 98 | + } |
| 99 | + return null; |
| 100 | + } |
| 101 | +//----------------------------------------------------------------------------------// |
| 102 | +} |
| 103 | +class HashDoubleApp { |
| 104 | + public static void main(String[] args) throws IOException |
| 105 | + { |
| 106 | + int aKey; |
| 107 | + DataItem aDataItem; |
| 108 | + int size, n; |
| 109 | + |
| 110 | + System.out.print("Enter size of hash table: "); |
| 111 | + size = getInt(); |
| 112 | + System.out.print("Enter initial number of items: "); |
| 113 | + n = getInt(); |
| 114 | + |
| 115 | + HashTable theHashTable = new HashTable(size); //make table |
| 116 | + |
| 117 | + for(int i = 0; i < n; i++) //insert data |
| 118 | + { |
| 119 | + aKey = (int)(java.lang.Math.random() * 2 * size); // between |
| 120 | + aDataItem = new DataItem(aKey); |
| 121 | + theHashTable.insert(aKey, aDataItem); |
| 122 | + } |
| 123 | + |
| 124 | + while(true) |
| 125 | + { |
| 126 | + System.out.print("Enter first letter of "); |
| 127 | + System.out.print("show, insert, delete, or find: "); |
| 128 | + char choice = getChar(); |
| 129 | + |
| 130 | + switch (choice) { |
| 131 | + case 's': |
| 132 | + theHashTable.displayTable(); |
| 133 | + break; |
| 134 | + case 'i': |
| 135 | + System.out.print("Enter key value to insert: "); |
| 136 | + aKey = getInt(); |
| 137 | + aDataItem = new DataItem(aKey); |
| 138 | + theHashTable.insert(aKey, aDataItem); |
| 139 | + break; |
| 140 | + case 'd': |
| 141 | + System.out.print("Enter key value to delete: "); |
| 142 | + aKey = getInt(); |
| 143 | + theHashTable.delete(aKey); |
| 144 | + break; |
| 145 | + case 'f': |
| 146 | + System.out.print("Enter key value to find: "); |
| 147 | + aKey = getInt(); |
| 148 | + aDataItem = theHashTable.find(aKey); |
| 149 | + if(aDataItem != null) |
| 150 | + System.out.println("Found " + aKey); |
| 151 | + else |
| 152 | + System.out.println("Could not find " + aKey); |
| 153 | + break; |
| 154 | + default: |
| 155 | + System.out.println("Invalid entry.\n"); |
| 156 | + } |
| 157 | + } |
| 158 | + } |
| 159 | + public static String getString() throws IOException |
| 160 | + { |
| 161 | + InputStreamReader isr = new InputStreamReader(System.in); |
| 162 | + BufferedReader br = new BufferedReader(isr); |
| 163 | + String s = br.readLine(); |
| 164 | + return s; |
| 165 | + } |
| 166 | + public static char getChar() throws IOException |
| 167 | + { |
| 168 | + String s = getString(); |
| 169 | + return s.charAt(0); |
| 170 | + } |
| 171 | + public static int getInt() throws IOException |
| 172 | + { |
| 173 | + String s = getString(); |
| 174 | + return Integer.parseInt(s); |
| 175 | + } |
| 176 | +} |
0 commit comments