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();
}
}
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
- hash-c-链地址法
- 数据结构 c语言 hash查找 链地址法实现
- 链地址法+动态开辟内存 hash
- Hash-开放地址法
- hash 开放地址法源码
- HASH链地址法和链表插入排序
- 链地址法实现一个很简单的hash表
- 哈希(hash) 之插入和查找(链地址法)
- hash 链地址法+ 数组模拟邻接表实现
- POJ 2002 Squares hash+链地址法处理冲突
- 一段采用链地址法解决冲突的Hash代码
- 哈希(hash) 之 hash的插入和查找(链地址法)
- Hash 取模法构造hash函数及链地址法防冲突
- 【hash表】两种实现(链地址法,开放地址法)
- HASH表(采用开放地址法处理hash冲突)
- hash(开放地址法解决冲突)
- 散列法(hash法、关键字地址计算法)
- POJ 1840 Hash 链地址避免冲突
- hash-B-使用在哈希法存储数据
- Mybatis实战之TypeHandler高级进阶
- storm中fieldsGrouping的分配策略实质
- Mybatis实战之自定义TypeHandler处理枚举
- Github Pages搭建自己写的页面
- hash-c-链地址法
- 排队打水问题(贪心)
- 深度学习框架性能评测(Caffe、CNTK、MXNet、TensorFlow、Torch)
- 北京云端微服科技2017笔试题
- 双目视觉的框架
- Linux其他命令
- 堆-A-基于完全二叉树实现,并且由数组存储的堆
- [可持久化可并堆 || ST表 点分治] BZOJ 3784 树上的路径
- SufaceView播放视频 进入后台重新进入不会播放