哈希表Java实现

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;
	}
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值