1. hash table Interface Class
Package cn.usst.hashtable;/** * Hash List interface class * @author G-xia * */public interface Hashtable {///to a hash list insert a keyword thekey element obj, if successful return True otherwise returns a false Boolean insert (object Thekey, Object obj),//To find and return the Thekey corresponding element of the given keyword in the hash list, if the lookup fails to return an empty object Search (object Thekey); Removes the element with the keyword Thekey from the hash list, or returns the false Boolean delete (Object thekey) If the deletion succeeds, or returns the number of elements that already exist in the hash list, int size ();//Returns the capacity of the hash table, That is, the space size of the hash table is the value of M int capacity ();//To determine whether the hash list is empty, or if NULL returns True otherwise false Boolean isEmpty ();//Clear the hash list of all elements to make it into an empty table void clear ();/ Output hash list of all keywords saved and corresponding elements void output ();}
2. Array storage class for dealing with conflicts using open address method
Package Cn.usst.hashtable.seqhashtable;import cn.usst.hashtable.hashtable;/** * Use linear probing to handle conflicts for hash storage * @author g-xia * */ public class Seqhashtable implements HashTable {private int m;//save Hash list capacity private object[] key; Defines an array of saved element keywords private object[] HT; Defines the array that holds the hash list private int n;//The number of elements that are already in the hash list private Object tag;//element content is deleted after the keyword delete tag//hash function, with the exception of private int h (Object thekey) {// Leave the remainder of the hair, if the argument is not an integer, should try to convert to an integer return (integer) thekey%m;} public seqhashtable (int mm, Object tag) {//Assuming that the hash list has a capacity of at least 13if (mm <) {m = 13;} else{m = mm;} N=0;key = new Object[m];ht = new Object[m];this.tag = tag;//keyword Removes the value marked as participating tag} @Overridepublic boolean insert (Object Thekey , Object obj) {int d = h (thekey); int temp = D;while (key[d]! = null && key[d].equals (TAG)! = True) {//Use linear probing method to deal with conflicts, find interpolation into position if (key[d].equals (thekey) = = true) break; Element already exists, exit loop D = (d+1)% m;if (d = = temp) {//Find a week after throw no position, should reorganize hash list or exit run System.out.println ("hash list no space, exit run"); System.exit (1);}} if (key[d] = = NULL | | key[d].equals (TAG) ==true) {//Find insertion position, insert new keyword and element and return true key[d] =Thekey;ht[d] = Obj;n++;return true;} else{//modifies an existing element with the new element obj and returns false Ht[d] = Obj;return false;}} @Overridepublic Object Search (object thekey) {int d= h (thekey); int temp = D;while (key[d]! = null) {if (Key[d].equals (Thekey ) {return ht[d];} Else{d = (d+1)% m;} if (d = = temp) return null;} return null;} @Overridepublic Boolean Delete (Object thekey) {int d = h (thekey); int temp = D;while (key[d]! = null) {if (Key[d].equals (Theke Y)) {key[d] = Tag;ht[d] = Null;n--;return true;} Else{d = (d+1)%m;} if (d==temp) {return false;}} return false;} @Overridepublic int size () {return n;} @Overridepublic int Capacity () {return m;} @Overridepublic Boolean isEmpty () {return n==0;} @Overridepublic void Clear () {for (int i=0; i<m; i++) {key[i] = null;ht[i] = null;} n=0;} @Overridepublic void Output () {for (int i=0; i<m; i++) {if (Key[i]==null | | key[i].equals (TAG)) continue; System.out.println ("(" + key[i] + "" + Ht[i] + "),"); System.out.println ();}}
3. Connection storage classes that use the chaining method to handle conflicts
Node type
Package cn.usst.hashtable.linkhashtable;/** * Type of node in the hash list with link-handling conflicts * @author G-xia * */public class Hashnode {Object Key;ob ject element; Hashnode next;public Hashnode (object Thekey, Object obj) {key = Thekey;element = Obj;next = null;}}
Implementation method
Package Cn.usst.hashtable.linkhashtable;import Cn.usst.hashtable.hashtable;public class Linkhashtable implements HashTable {private int m;//save hash List's capacity private hashnode[] ht;//defines the array that holds the hash list private int n;//The number of elements already in the hash list//hash function private int h ( Object Thekey) {return (Integer) Thekey% m;} public linkhashtable (int mm) {if (mm <) {m = 13;} else{m = mm;} n = 0; HT = new Hashnode[m];} @Overridepublic Boolean Insert (Object Thekey, Object obj) {int d = h (thekey); Hashnode p = ht[d];//the node in the order of the single-linked list looks for nodes while (P! = null) {if (p.key.equals (thekey) = = True) Thekey break;elsep = P.next;} if (p = null) {//with new element obj modifies an existing node's element value and returns false p.element = Obj;return false;} Else{p = new Hashnode (Thekey, obj);p. Next = ht[d];ht[d] = P;n++;return true;}} @Overridepublic Object Search (object thekey) {int d = h (thekey); Hashnode p = ht[d];while (p!=null) {if (P.key.equals (Thekey)) return p.element;elsep = P.next;} return null;} @Overridepublic Boolean Delete (Object thekey) {int d = h (thekey); Hashnode p = ht[d], q = null;//p points to the table head node, Q points to the precursor node, initially empty while (P! = NuLL) {if (P.key.equals (thekey)) break;else{q = p; p = P.next;}} if (p = = null)//element not deleted, return Falsereturn false;else if (q = = null)//Delete header node Ht[d] = p.next;else//Delete non-table header node Q.next = p.next;n-- ; return true;} @Overridepublic int size () {return n;} @Overridepublic int Capacity () {return m;} @Overridepublic Boolean isEmpty () {return n==0;} @Overridepublic void Clear () {for (int i=0; i<m; i++) {ht[i] = null;} n=0;} @Overridepublic void Output () {for (int i=0; i<m; i++) {Hashnode p = ht[i];while (P! = null) {System.out.println ("(" + p.ke Y + "" + P.element + "),");p = P.next;}} System.out.println ();}}
4. Test method
public class seqhashtabletest {@Testpublic void Seqhashtabletest () {int[] a = {18, 75, 60, 43, 54, 90, 46, 31, 58, 73, 15, 34}; String[] B = {"180", "750", "600", "420", "540", "900", "460", "310", "580", "730", "150", "340"}; HashTable TB = new Seqhashtable (1),//hashtable TB = new Linkhashtable (n), for (int i=0; i<a.length; i++) {Tb.insert ( A[i], b[i]);} System.out.println ("All elements in the output hash list:"); Tb.output (); SYSTEM.OUT.PRINTLN ("Size of the hash table:" + tb.capacity ()); System.out.println ("Number of elements in the hash table:" + tb.size ()); for (int i=0; i<a.length; i+=3) {tb.delete (a[i]);} Tb.insert ("880"); Tb.insert (75, "7500"); SYSTEM.OUT.PRINTLN ("After inserting, deleting, modifying, the hash list is:"); Tb.output (); SYSTEM.OUT.PRINTLN ("Size of the hash table:" + tb.capacity ()); System.out.println ("Number of elements in the hash table:" + tb.size ()); for (int i=0; i<4; i++) {String x = (String) (Tb.search (A[i])); if (x! = null ) System.out.println (A[i] + "" + x); ElseSystem.out.println (A[i] + "element not found for the keyword! ");}}}
Hashtable arrays and connections two implementations (Java version)