Unit 8 Chapter 11 Hash Tables Code Flashcards
1
Q
hashDouble
A
import java.io.*; //////////////////////////////////////////////////////////////// class DataItem { // (could have more items) private int iData; // data item (key) //-------------------------------------------------------------- public DataItem(int ii) // constructor { iData = ii; } //-------------------------------------------------------------- public int getKey() { return iData; } //-------------------------------------------------------------- } // end class DataItem //////////////////////////////////////////////////////////////// class HashTable { private DataItem[] hashArray; // array is the hash table private int arraySize; private DataItem nonItem; // for deleted items // ------------------------------------------------------------- HashTable(int size) // constructor { arraySize = size; hashArray = new DataItem[arraySize]; nonItem = new DataItem(-1); } // ------------------------------------------------------------- public void displayTable() { System.out.print("Table: "); for(int j=0; j); } // end switch } // end while } // end main() //-------------------------------------------------------------- public static String getString() throws IOException { InputStreamReader isr = new InputStreamReader(System.in); BufferedReader br = new BufferedReader(isr); String s = br.readLine(); return s; } //-------------------------------------------------------------- public static char getChar() throws IOException { String s = getString(); return s.charAt(0); } //------------------------------------------------------------- public static int getInt() throws IOException { String s = getString(); return Integer.parseInt(s); } //-------------------------------------------------------------- } // end class HashDoubleApp ////////////////////////////////////////////////////////////////
2
Q
hashChain
A
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; //////////////////////////////////////////////////////////////// class Link { // (could be other items) private int iData; // data item public Link next; // next link in list // ------------------------------------------------------------- public Link(int it) // constructor { iData= it; } // ------------------------------------------------------------- public int getKey() { return iData; } // ------------------------------------------------------------- public void displayLink() // display this link { System.out.print(iData + " "); } } // end class Link //////////////////////////////////////////////////////////////// class SortedList { private Link first; // ref to first list item // ------------------------------------------------------------- public void SortedList() // constructor { first = null; } // ------------------------------------------------------------- public void insert(Link theLink) // insert link, in order { int key = theLink.getKey(); Link previous = null; // start at first Link current = first; // until end of list, while( current != null && key > current.getKey() ) { // or current > key, previous = current; current = current.next; // go to next item } if(previous==null) // if beginning of list, first = theLink; // first --> new link else // not at beginning, previous.next = theLink; // prev --> new link theLink.next = current; // new link --> current } // end insert() // ------------------------------------------------------------- public void delete(int key) // delete link { // (assumes non-empty list) Link previous = null; // start at first Link current = first; // until end of list, while( current != null && key != current.getKey() ) { // or key == current, previous = current; current = current.next; // go to next link } // disconnect link if(previous==null) // if beginning of list first = first.next; // delete first link else // not at beginning previous.next = current.next; // delete current link } // end delete() // ------------------------------------------------------------- public Link find(int key) // find link { Link current = first; // start at first // until end of list, while(current != null && current.getKey() ); } // end switch } // end while } // end main() //-------------------------------------------------------------- public static String getString() throws IOException { InputStreamReader isr = new InputStreamReader(System.in); BufferedReader br = new BufferedReader(isr); String s = br.readLine(); return s; } //------------------------------------------------------------- public static char getChar() throws IOException { String s = getString(); return s.charAt(0); } //------------------------------------------------------------- public static int getInt() throws IOException { String s = getString(); return Integer.parseInt(s); } //-------------------------------------------------------------- } // end class HashChainApp ////////////////////////////////////////////////////////////////
3
Q
hash
A
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; //////////////////////////////////////////////////////////////// class DataItem { // (could have more data) private int iData; // data item (key) //-------------------------------------------------------------- public DataItem(int ii) // constructor { iData = ii; } //-------------------------------------------------------------- public int getKey() { return iData; } //-------------------------------------------------------------- } // end class DataItem //////////////////////////////////////////////////////////////// class HashTable { private DataItem[] hashArray; // array holds hash table private int arraySize; private DataItem nonItem; // for deleted items // ------------------------------------------------------------- public HashTable(int size) // constructor { arraySize = size; hashArray = new DataItem[arraySize]; nonItem = new DataItem(-1); // deleted item key is -1 } // ------------------------------------------------------------- public void displayTable() { System.out.print("Table: "); for(int j=0; j); } // end switch } // end while } // end main() //-------------------------------------------------------------- public static String getString() throws IOException { InputStreamReader isr = new InputStreamReader(System.in); BufferedReader br = new BufferedReader(isr); String s = br.readLine(); return s; } //-------------------------------------------------------------- public static char getChar() throws IOException { String s = getString(); return s.charAt(0); } //------------------------------------------------------------- public static int getInt() throws IOException { String s = getString(); return Integer.parseInt(s); } //-------------------------------------------------------------- } // end class HashTableApp ////////////////////////////////////////////////////////////////