|
| 1 | +package Hashtable; |
| 2 | +import java.io.*; |
| 3 | + |
| 4 | +class Link |
| 5 | +{ |
| 6 | + private int iData;// data item |
| 7 | + public Link next; // next link in list |
| 8 | +//----------------------------------------------------------------------------------// |
| 9 | + public Link(int ii) |
| 10 | + { |
| 11 | + iData = ii; |
| 12 | + } |
| 13 | +//----------------------------------------------------------------------------------// |
| 14 | + public int getKey() |
| 15 | + { |
| 16 | + return iData; |
| 17 | + } |
| 18 | +//----------------------------------------------------------------------------------// |
| 19 | + public void displayLink() |
| 20 | + { |
| 21 | + System.out.print(iData + " "); |
| 22 | + } |
| 23 | +} |
| 24 | +class SortedList |
| 25 | +{ |
| 26 | + private Link first; |
| 27 | +//----------------------------------------------------------------------------------// |
| 28 | + public SortedList() |
| 29 | + { |
| 30 | + first = null; |
| 31 | + } |
| 32 | +//----------------------------------------------------------------------------------// |
| 33 | + public void insert(Link theLink) |
| 34 | + { |
| 35 | + int key = theLink.getKey(); |
| 36 | + Link previous = null; |
| 37 | + Link current = first; //start at first |
| 38 | + |
| 39 | + while( current != null && key > current.getKey() ) // until the end of the list or current > key |
| 40 | + { |
| 41 | + previous = current; |
| 42 | + current = current.next; // go to next item |
| 43 | + } |
| 44 | + if(previous == null ) //if beginning of list, first --> newLink |
| 45 | + first = theLink; |
| 46 | + else // not at beginning, |
| 47 | + previous.next = theLink; // prev --> newLink |
| 48 | + theLink.next = current; // newLink --> current |
| 49 | + } |
| 50 | +//----------------------------------------------------------------------------------// |
| 51 | + public void delete(int key) |
| 52 | + { |
| 53 | + Link previous = null; |
| 54 | + Link current = first; |
| 55 | + |
| 56 | + while( current != null && key != current.getKey() ) |
| 57 | + { |
| 58 | + previous = current; |
| 59 | + current = current.next; |
| 60 | + } |
| 61 | + |
| 62 | + if(previous == null) |
| 63 | + first = first.next; |
| 64 | + else |
| 65 | + previous.next = current.next; |
| 66 | + } |
| 67 | +//----------------------------------------------------------------------------------// |
| 68 | + public Link find(int key) |
| 69 | + { |
| 70 | + Link current = first; |
| 71 | + |
| 72 | + while (current != null && current.getKey() <= key) { |
| 73 | + if(current.getKey() == key) |
| 74 | + return current; |
| 75 | + current = current.next; |
| 76 | + } |
| 77 | + return null; |
| 78 | + } |
| 79 | +//----------------------------------------------------------------------------------// |
| 80 | + public void displayList() |
| 81 | + { |
| 82 | + System.out.print("List (first-->last): "); |
| 83 | + Link current = first; |
| 84 | + while(current != null) |
| 85 | + { |
| 86 | + current.displayLink(); |
| 87 | + current = current.next; |
| 88 | + } |
| 89 | + System.out.println(""); |
| 90 | + } |
| 91 | +} |
| 92 | +class HashTable |
| 93 | +{ |
| 94 | + private SortedList[] hashArray; |
| 95 | + private int arraySize; |
| 96 | +//----------------------------------------------------------------------------------// |
| 97 | + public HashTable(int size) |
| 98 | + { |
| 99 | + arraySize = size; |
| 100 | + hashArray = new SortedList[arraySize]; |
| 101 | + for(int i = 0; i < arraySize; i++) |
| 102 | + hashArray[i] = new SortedList(); |
| 103 | + } |
| 104 | +//----------------------------------------------------------------------------------// |
| 105 | + public void displayTable() |
| 106 | + { |
| 107 | + for(int i = 0; i < arraySize; i++) |
| 108 | + { |
| 109 | + System.out.print(i + ", "); |
| 110 | + hashArray[i].displayList(); |
| 111 | + } |
| 112 | + } |
| 113 | +//----------------------------------------------------------------------------------// |
| 114 | + public int hashFunc(int key) |
| 115 | + { |
| 116 | + return key % arraySize; |
| 117 | + } |
| 118 | +//----------------------------------------------------------------------------------// |
| 119 | + public void insert(Link theLink) |
| 120 | + { |
| 121 | + int key = theLink.getKey(); |
| 122 | + int hashVal = hashFunc(key); //hash the key |
| 123 | + hashArray[hashVal].insert(theLink); //insert at hashVal |
| 124 | + } |
| 125 | +//----------------------------------------------------------------------------------// |
| 126 | + public void delete(int key) |
| 127 | + { |
| 128 | + int hashVal = hashFunc(key); //hash the key |
| 129 | + hashArray[hashVal].delete(key); // delete link |
| 130 | + } |
| 131 | +//----------------------------------------------------------------------------------// |
| 132 | + public Link find(int key) |
| 133 | + { |
| 134 | + int hashVal = hashFunc(key); //hash the key |
| 135 | + Link theLink = hashArray[hashVal].find(key); //get link |
| 136 | + return theLink; |
| 137 | + } |
| 138 | +//----------------------------------------------------------------------------------// |
| 139 | +} |
| 140 | +class HashChainApp { |
| 141 | + public static void main(String[] args) throws IOException |
| 142 | + { |
| 143 | + int aKey; |
| 144 | + Link aDataItem; |
| 145 | + int size, n, keysPerCell = 100; |
| 146 | + |
| 147 | + System.out.print("Enter size of the hash table: "); |
| 148 | + size = getInt(); |
| 149 | + System.out.print("Enter initial number of items: "); |
| 150 | + n = getInt(); |
| 151 | + |
| 152 | + HashTable theHashTable = new HashTable(size); |
| 153 | + |
| 154 | + for(int i = 0; i < n; i++) |
| 155 | + { |
| 156 | + aKey = (int)(java.lang.Math.random() * keysPerCell * size); |
| 157 | + aDataItem = new Link(aKey); |
| 158 | + theHashTable.insert(aDataItem); |
| 159 | + } |
| 160 | + |
| 161 | + while(true) |
| 162 | + { |
| 163 | + System.out.print("Emter first leter of "); |
| 164 | + System.out.print("show, insert, delete, or find: "); |
| 165 | + char choice = getChar(); |
| 166 | + switch(choice) |
| 167 | + { |
| 168 | + case 's': |
| 169 | + theHashTable.displayTable(); |
| 170 | + break; |
| 171 | + case 'i': |
| 172 | + System.out.print("Enter key value to insert: "); |
| 173 | + aKey = getInt(); |
| 174 | + aDataItem = new Link(aKey); |
| 175 | + theHashTable.insert(aDataItem); |
| 176 | + break; |
| 177 | + case 'd': |
| 178 | + System.out.print("Enter key value to delete: "); |
| 179 | + aKey = getInt(); |
| 180 | + theHashTable.delete(aKey); |
| 181 | + break; |
| 182 | + case 'f': |
| 183 | + System.out.print("Enter key value to find: "); |
| 184 | + aKey = getInt(); |
| 185 | + aDataItem = theHashTable.find(aKey); |
| 186 | + if(aDataItem != null) |
| 187 | + System.out.print("Found " + aKey); |
| 188 | + else |
| 189 | + System.out.println("Could not find " + aKey); |
| 190 | + break; |
| 191 | + default: |
| 192 | + System.out.print("Invalid Entry\n"); |
| 193 | + } |
| 194 | + } |
| 195 | + } |
| 196 | +//----------------------------------------------------------------------------------// |
| 197 | + public static String getString() throws IOException |
| 198 | + { |
| 199 | + InputStreamReader isr = new InputStreamReader(System.in); |
| 200 | + BufferedReader br = new BufferedReader(isr); |
| 201 | + String s = br.readLine(); |
| 202 | + return s; |
| 203 | + } |
| 204 | +//----------------------------------------------------------------------------------// |
| 205 | + public static char getChar()throws IOException |
| 206 | + { |
| 207 | + String s = getString(); |
| 208 | + return s.charAt(0); |
| 209 | + } |
| 210 | +//----------------------------------------------------------------------------------// |
| 211 | + public static int getInt() throws IOException |
| 212 | + { |
| 213 | + String s = getString(); |
| 214 | + return Integer.parseInt(s); |
| 215 | + } |
| 216 | +} |
0 commit comments