public class HashTableDemo {
public static void main(String[] args) {
LinkedListArray hashListArray = new LinkedListArray(2);
String key = "";
Scanner scanner = new Scanner(System.in);
while (true) {
System.out.println("add:添加雇员");
System.out.println("list: 遍历链表数组");
System.out.println("find:查找雇员根据id");
System.out.println("delete:删除雇员根据id");
System.out.println("exit:退出系统");
key = scanner.next();
switch (key) {
case "add":
System.out.println("输入id");
int id = scanner.nextInt();
hashListArray.add(id);
break;
case "list":
hashListArray.showAll();
break;
case "find":
System.out.println("输入编号");
int no = scanner.nextInt();
Node emp = hashListArray.find(no);
if (emp != null)
System.out.println(emp.data);
else {
System.out.println("找不到此员工");
}
break;
case "delete":
System.out.println("输入删除员工id");
int no1 = scanner.nextInt();
int result = hashListArray.del(no1);
if (result == 1)
System.out.println("删除成功");
else {
System.out.println("删除失败");
}
break;
case "exit":
System.out.println("退出系统");
System.exit(0);
break;
}
}
}
}
class LinkedListArray {
LinkedList[] linkedListArray;
int length;
public void showAll() {
if (length == 0) {
System.out.println("链表数组为空");
return;
}
for (LinkedList linkedList : linkedListArray) {
linkedList.showAll();
System.out.println();
}
}
public LinkedListArray(int length) {
this.length = length;
linkedListArray = new LinkedList[length];
for (int i = 0; i < linkedListArray.length; i++) {
linkedListArray[i] = new LinkedList();
}
}
public Node find(int data) {
int index = hash(data);
return linkedListArray[index].find(data);
}
public int del(int data) {
int index = hash(data);
return linkedListArray[index].del(data);
}
public void add(int data) {
int index = hash(data);
linkedListArray[index].addAtRear(data);
}
public int hash(int data) {
return data % length;
}
}
class LinkedList {
Node head;
public void showAll() {
if (isEmpty()) {
System.out.println("链表为空");
}
Node current = head;
while (current != null) {
System.out.print(current.data + " ");
current = current.next;
}
}
public Node find(int data) {
if (isEmpty())
System.out.println("链表为空");
Node current = head;
while (current != null) {
if (current.data == data) {
return current;
}
current = current.next;
}
return null;
}
public int del(int data) {
if (isEmpty()) {
throw new RuntimeException("链表为空");
}
if (head.data == data) {
head = head.next;
return 1;
}
Node current = head;
while (current.next != null) {
if (current.next.data == data) {
current.next = current.next.next;
return 1;
}
current = current.next;
}
return -1;
}
public void addAtFront(int data) {
Node node = new Node(data);
if (isEmpty()) {
head = node;
return;
}
node.next = head;
head = node;
}
public void addAtRear(int data) {
Node node = new Node(data);
if (isEmpty()) {
head = node;
return;
}
Node current = head;
while (current.next != null) {
current = current.next;
}
current.next = node;
}
public boolean isEmpty() {
return head == null;
}
}
class Node {
int data;
Node next;
public Node(int data) {
this.data = data;
}
}
哈希表Java实现
最新推荐文章于 2024-02-15 23:53:12 发布