hash-c-链地址法

来源:互联网 发布:c语言ide 轻便 编辑:程序博客网 时间:2024/04/28 01:49
package i_hash.C_hashChain;


import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;


/**
 * 链地址法
 * 
 * @author Administrator
 *
 */
public class HashChainApp {
public static void main(String[] args) throws NumberFormatException,
IOException {
int aKey, size, n, keysPerCell = 100;
Link aDataItem;
System.out.print("指定哈希表的大小:");
size = getInt();
System.out.print("指定哈希表初始数目:");
n = getInt();
HashTable theHashTable = new HashTable(size);
for (int i = 0; i < n; i++) {
aKey = (int) (Math.random() * keysPerCell * size);
aDataItem = new Link(aKey);
theHashTable.insert(aDataItem);
}
while (true) {
System.out.print("请输入第一个字母:");
System.out.print("show,find,insert or delete ");
char choice = getChar();
switch (choice) {
case 's':
theHashTable.displayTable();
break;
case 'i':
System.out.print("请输入要插入的数据:");
aKey = getInt();
aDataItem = new Link(aKey);
theHashTable.insert(aDataItem);
break;
case 'd':
System.out.print("请输入要插入的数据:");
aKey = getInt();
theHashTable.delete(aKey);
break;
case 'f':
System.out.print("请输入要查询的数据");
aKey = getInt();
aDataItem = theHashTable.find(aKey);
if (aDataItem != null)
System.out.println("找到" + aKey);
else
System.out.println("查找失败" + aKey);
break;


default:
System.out.println("无效操作");
}
}


}


private static char getChar() throws IOException {
return getString().charAt(0);
}


private static String getString() throws IOException {
return new BufferedReader(new InputStreamReader(System.in)).readLine();
}


private static int getInt() throws NumberFormatException, IOException {
return Integer.parseInt(getString());
}
}package i_hash.C_hashChain;


public class HashTable {
private SortedList[] hashArray;
private int arraySize;


public HashTable(int size) {
arraySize = size;
hashArray = new SortedList[arraySize];
// 填满数组
for (int i = 0; i < arraySize; i++) {
hashArray[i] = new SortedList();
}
}


public void displayTable() {
for (int i = 0; i < arraySize; i++) {
System.out.print(i + " ");
hashArray[i].displayList();
}
}


public int hashFunc(int key) {
return key % arraySize;
}


public void insert(Link theLink) {
int key = theLink.getKey();
int hashVal = hashFunc(key);
hashArray[hashVal].insert(theLink);
}


public void delete(int key) {
int hashVal = hashFunc(key);
hashArray[hashVal].delete(key);
}


public Link find(int key) {
int hashVal = hashFunc(key);
Link theLink = hashArray[hashVal].find(key);
return theLink;
}


}package i_hash.C_hashChain;


public class Link {
private int iData;
public Link next;


public Link(int iData) {
this.iData = iData;
}


public int getKey() {
return iData;
}


public void displayLink() {
System.out.print(iData + " ");
}


}package i_hash.C_hashChain;


public class SortedList {
private Link first;


public SortedList() {
first = null;
}


public void insert(Link theLink) {
int key = theLink.getKey();
Link previous = null;
Link current = first;


while (current != null && key > current.getKey()) {
previous = current;
current = current.next;
}
if (previous == null)
first = theLink;
else
previous.next = theLink.next;


theLink.next = current;


}


public void delete(int key) {
Link previous = null;
Link current = first;
while (current != null && key != current.getKey()) {
previous = current;
current = current.next;
}
if (previous == null)
first = first.next;
else
previous.next = current.next;
}


public Link find(int key) {
Link current = first;
while (current != null && current.getKey() <= key) {
if (current.getKey() == key)
return current;
current = current.next;
}
return null;
}


public void displayList() {
Link current = first;
while (current != null) {
current.displayLink();
current = current.next;
}
System.out.println();
}


}
0 0
原创粉丝点击