1. 哈希表基本思路
- 建立一个数组,存放若干链表
- 添加节点时,通过哈希函数,确定应当放入的链表,并将节点插入该链表
2. 基本结构
2.1 SingleNode
- int id
- int/String data
- SingleNode nextSingleNode
2.2 SingleLinkedList
- SingleNode headNode
- isEmpty()
- addNode()
- displayAllNodeData()
- findNode()
- deleteNode()
2.3 HashTable
- Constructor
- hashFunction()
- addData()
- displayAllData()
- findNode()
- deleteNode()
3. 代码实现
public class HashTable {
private HashTableSingleLinkedList[] hashTableSingleLinkedListArray;
private final int arraySize;
public HashTable(int arraySize) {
this.arraySize = arraySize;
this.hashTableSingleLinkedListArray = new HashTableSingleLinkedList[arraySize];
for (int i = 0; i < arraySize; i++) {
this.hashTableSingleLinkedListArray[i] = new HashTableSingleLinkedList();
}
}
public int hashFunction(int id) {
return id % this.arraySize;
}
public void addData(int id, String data) {
if (id < 0) {
throw new RuntimeException("id should be larger than 0");
}
HashTableSingleNode newNode = new HashTableSingleNode(id, data);
this.hashTableSingleLinkedListArray[hashFunction(id)].addNode(newNode);
}
public void displayAllData(){
for (int i = 0; i < this.arraySize; i++) {
System.out.println("List [" + i + "]");
if (!hashTableSingleLinkedListArray[i].isEmpty()) {
hashTableSingleLinkedListArray[i].displayAllNodeData();
} else {
System.out.println("---- null ----");
}
}
}
public HashTableSingleNode findNode(int id) {
if (id < 0) {
throw new RuntimeException("id < 0");
}
return this.hashTableSingleLinkedListArray[hashFunction(id)].findNode(id);
}
public void deleteNode(int id){
if (id < 0) {
throw new RuntimeException("id < 0");
}
this.hashTableSingleLinkedListArray[hashFunction(id)].deleteNode(id);
}
}
class HashTableSingleLinkedList {
private HashTableSingleNode headNode;
public boolean isEmpty() {
return headNode == null;
}
public void addNode(HashTableSingleNode newNode) {
if (newNode == null) {
throw new RuntimeException("new node null");
}
HashTableSingleNode tempNode = headNode;
if (headNode == null || headNode.getId() > newNode.getId()) {
headNode = newNode;
newNode.setNextNode(tempNode);
}
else if (headNode.getId() == newNode.getId()){
throw new RuntimeException("exist");
}
else {
HashTableSingleNode currentNode = headNode;
while (currentNode.getNextNode() != null) {
if (currentNode.getNextNode().getId() > newNode.getId()) {
tempNode = currentNode.getNextNode();
currentNode.setNextNode(newNode);
newNode.setNextNode(tempNode);
return;
}
else if (currentNode.getNextNode().getId() == newNode.getId()) {
throw new RuntimeException("exist");
}
currentNode = currentNode.getNextNode();
}
currentNode.setNextNode(newNode);
}
}
public void displayAllNodeData() {
if (headNode == null) {
throw new RuntimeException("empty");
}
HashTableSingleNode currentNode = headNode;
while (currentNode != null) {
System.out.println(currentNode.toString());
currentNode = currentNode.getNextNode();
}
}
public HashTableSingleNode findNode(int id) {
if (headNode == null) {
throw new RuntimeException("empty");
}
HashTableSingleNode currentNode = headNode;
while (currentNode != null && currentNode.getId() != id) {
currentNode = currentNode.getNextNode();
}
return currentNode;
}
public void deleteNode(int id) {
if (headNode == null) {
throw new RuntimeException("empty");
}
HashTableSingleNode currentNode = headNode;
HashTableSingleNode preNode = null;
while (currentNode != null) {
if (currentNode.getId() == id) {
if (preNode == null) {
headNode = headNode.getNextNode();
} else {
preNode.setNextNode(currentNode.getNextNode());
}
break;
}
preNode = currentNode;
currentNode = currentNode.getNextNode();
}
if (currentNode == null) {
throw new RuntimeException("not found");
}
}
}
class HashTableSingleNode {
private int id;
private String data;
private HashTableSingleNode nextNode;
public HashTableSingleNode(int id, String data) {
this.id = id;
this.data = data;
}
@Override
public String toString() {
return "HashTableSingleNode{" +
"id=" + id +
", data='" + data + '\'' +
'}';
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getData() {
return data;
}
public void setData(String data) {
this.data = data;
}
public HashTableSingleNode getNextNode() {
return nextNode;
}
public void setNextNode(HashTableSingleNode nextNode) {
this.nextNode = nextNode;
}
}