Package cn.com.chenlly;
Import java.io.IOException;
Import Java.io.InputStreamReader;
Import Java.io.BufferedReader;
/**
* @Description The chain address method algorithm when a hash table conflicts, the keyword is mapped to a hash function and inserted into the list of cells
* Fill factor: The ratio of data item to hash table capacity, in the development address method is 1, in the chain address method >=1 Bucket: If the hash table is linked to an array,
* Then such an array is called a bucket.
* @Company OpenData
* @Author chenlly
* @Version 1.0
* @Data 09-03-31
*/
Class Link {
public int idata;
Public Link Next;
Construction method
Public Link (int ndata) {
This.idata = Ndata;
}
}
Class Linklist {
Private Link A;
Construction method
Public linklist () {
i = null;
}
Inserting data items into a linked list
public void Insert (link link) {
Link current = i;
Link previous = null;
while (current!= null && link.idata > Current.idata) {
previous = current;
current = Current.next;
}
Handling of the first node (header interpolation method)
if (previous = = null) {
i = link;
} else {
Previous.next = link;
}
Link.next = current;
}
Search for key-word nodes in a list
Public Link find (int key) {
Link current = i;
While (the current!= null && current.idata <= key) {
if (Current.idata!= key) {
current = Current.next;
} else {
return to current;
}
}
return null;
}
Delete a keyword corresponding node in a linked list
public void Delete (int key) {
Link current = i;
Link previous = null;
Because the hash table unit has no data items
if (current = = null) {
return;
}
While (the current!= null && current.idata!= key) {
Because at the end of the chain
if (Current.next = = null) {
Return
}
previous = current;
current = Current.next;
}
if (previous = = null) {
i = First.next;
} else {
Previous.next = Current.next;
}
}
Display linked list information
public void display () {
System.out.print ("List (First->end)");
Link current = i;
while (current!= null) {
System.out.print (Current.idata + ",");
current = Current.next;
}
System.out.println ("");
}
}
public class Hashlinkapp {
private static final int max_size = 20;
Private linklist[] Hasharray;
Construction method
public Hashlinkapp (int size) {
Hasharray = new Linklist[size];
Initializing a hash table
for (int i = 0; i < hasharray.length; i++) {
Hasharray[i] = new linklist ();
}
}
hash function
public int hashfun (int key) {
Return key% Hasharray.length;
}
Keyword Insert hash table
public void Insert (link link) {
int hashval = This.hashfun (Link.idata);
Hasharray[hashval].insert (link);
}
Find in a hash table by keyword
Public Link FindByID (int key) {
int hashval = This.hashfun (key);
Link link = hasharray[hashval].find (key);
return link;
}
Delete data items by keyword
public void Delete (int key) {
int hashval = This.hashfun (key);
Hasharray[hashval].delete (key);
}
Display hash table data item information
public void Displayhasht () {
for (int i = 0; i < hasharray.length && hasharray[i]!= null; i++) {
System.out.print (i + ".");
Hasharray[i].display ();
}
}
reading data from the keyboard
public static String Readstr () {
InputStreamReader ir = new InputStreamReader (system.in);
BufferedReader br = new BufferedReader (IR);
String str = "";
try {
str = Br.readline ();
catch (IOException ex) {
Ex.printstacktrace ();
}
return str;
}
Keynote function
public static void Main (string[] args) {
SYSTEM.OUT.PRINTLN ("Please select Action sequence");
System.out.println ("1 Insert data entry to hash table");
System.out.println ("2 Search by keyword");
System.out.println ("3 delete keyword for data item in hash table");
System.out.println ("4 shows the data item information in the hash table");
System.out.println ("5 exit");
SYSTEM.OUT.PRINTLN ("Please input hash table space size");
int size = Integer.parseint (Hashlinkapp.readstr ());
Hashlinkapp Hashlinkapp = new Hashlinkapp (size);
Link Itemlink;
while (true) {
System.out.println ("Please enter the sequence of operations");
int op = integer.parseint (Hashlinkapp.readstr ());
Switch (OP) {
Case 1:
System.out.println ("Please enter data item");
int key = Integer.parseint (Hashlinkapp.readstr ());
Itemlink = new Link (key);
Hashlinkapp.insert (Itemlink);
Break
Case 2:
System.out.println ("Please enter the data item you want to find");
int fkey = Integer.parseint (Hashlinkapp.readstr ());
Link link = Hashlinkapp.findbyid (fkey);
if (link!= null) {
SYSTEM.OUT.PRINTLN ("Data item" + Link.idata + "already found");
} else {
SYSTEM.OUT.PRINTLN ("Data item not found");
}
Break
Case 3:
System.out.println ("Please enter the data item you want to delete");
int dkey = Integer.parseint (Hashlinkapp.readstr ());
Hashlinkapp.delete (Dkey);
Break
Case 4:
Hashlinkapp.displayhasht ();
Break
Case 5:
Default
System.exit (0);
}//End Switch
}//End While
}
}