哈希表
public class HashTableDemo {
public static void main(String[] args) {
HashTable hashTable = new HashTable(7);
String key = "";
Scanner scanner = new Scanner(System.in);
while (true){
System.out.printf("add :添加节点 ");
System.out.printf("list :显示所有节点 ");
System.out.printf("find :查找节点 ");
System.out.printf("del :删除节点 ");
System.out.printf("exit :退出程序
");
key = scanner.next();
switch (key){
case "add" :
System.out.println("请输入id:");
int id = scanner.nextInt();
System.out.println("请输入name:");
String name = scanner.next();
Node node = new Node(id, name);
hashTable.add(node);
break;
case "list" :
hashTable.list();
break;
case "exit" :
scanner.close();
System.exit(0);
case "find" :
System.out.print("请输入要查找的id:");
id = scanner.nextInt();
hashTable.findNode(id);
break;
case "del" :
System.out.print("请输入要删除的id:");
id = scanner.nextInt();
hashTable.delNode(id);
default:
break;
}
}
}
}
class Node{
public int id;
public String name;
public Node next;
public Node(int id, String name) {
this.id = id;
this.name = name;
}
}
class LinkedList{
private Node head;
//添加节点
public void add(Node node){
if (head == null){
head = node;
return;
}
//声明一个辅助指针,定位到最后
Node temp = head;
while (true){
if (temp.next == null){
break;
}
temp = temp.next;
}
temp.next = node;
}
//遍历链表
public void list(int no){
if (head == null){
System.out.println("第"+no+"条链表为空!");
return;
}
System.out.print("第"+no+"条链表信息为:");
Node temp = head;
while (true){
System.out.printf("=> id =%d name=%s " , temp.id , temp.name);
if (temp.next == null){
break;
}
temp = temp.next;
}
System.out.println();
}
//查找
public Node findNode(int id){
if (head == null){
System.out.println("链表为空");
return null;
}
Node temp = head;
while (true){
if (temp.id == id){
break;
}
if (temp.next == null){
temp = null;//改变了最后一个数
break;
}
temp = temp.next;
}
return temp;
}
//删除
public void delNode(int id){
if (head == null){
System.out.println("链表为空,你要删除的节点不存在");
return;
}
Node temp = head;
while (true){
if (head.id == id){
head = head.next;
System.out.println("删除成功");
break;
}
if (temp.next == null){
System.out.println("要删除的节点不存在");
break;
}
if (temp.next.id == id){
temp.next = temp.next.next;
System.out.println("删除成功");
break;
}
temp = temp.next;
}
}
}
//创建哈希表
class HashTable{
private LinkedList[] linkedListsArr;
private int size;//表示有多少条链表
public HashTable(int size) {
this.size = size;
linkedListsArr = new LinkedList[size];
for (int i = 0; i < size; i++) {
linkedListsArr[i] = new LinkedList();
}
}
//添加节点
public void add(Node node){
int linkListNo = hashFun(node.id);
linkedListsArr[linkListNo].add(node);
}
//编写散列函数,取模法
public int hashFun(int id){
return id % size;
}
//遍历哈希表
public void list(){
for (int i = 0; i < size; i++) {
linkedListsArr[i].list(i);
}
}
//查找
public void findNode(int id){
int linkListNo = hashFun(id);
Node node = linkedListsArr[linkListNo].findNode(id);
if (node != null){
System.out.println("在第"+(linkListNo+1)+"条链表找到该节点");
}else {
System.out.println("在哈希表中没有找到该节点");
}
}
//删除节点
public void delNode(int id){
int linkListNo = hashFun(id);
linkedListsArr[linkListNo].delNode(id);
}
}