您的位置:首页 > 理论基础 > 数据结构算法

数据结构算法爬坑日记十

2020-08-25 18:17 1226 查看

哈希表

哈希表(hash table)

哈希表:也叫散列表,通过将关键码值映射到表中的一个位置来访问,加快查找的速度,映射函数也叫散列函数,
存放记录的数组叫散列表,在对一些不常改变的数据进行操作的时候,为了加快速度,通常会采用缓存技术将其加载
到内存中进行操作,而不直接对数据库进行操作,如redis,散列表就相当于一个简化版redis,可以通过数组+链表
或数组+二叉树来实现

示例题:一个公司,当有新员工来报道时,将该员工的信息加入,输入员工id时,查找到员工信息(要求不使用数据库,尽量节省内存,速度尽量快)

代码实现(数组+链表方式)
** 链表节点类
//链表节点类
public class LinkedListNode {
int id;
String name;
LinkedListNode next;

public LinkedListNode(int id, String name) {
this.id = id;
this.name = name;
}
}

** 链表类
//链表类
public class LinkedList {
LinkedListNode head;//头节点

//添加数据
public void add(LinkedListNode node) {
if (head == null) {
head = node;
} else {
LinkedListNode current = head;//辅助指针
while (current.next != null) {
current = current.next;
}
current.next = node;
}
}

//查看数据
public void show(int num) {
if (head == null) {
System.out.println("第" + (num + 1) + "条链表为空");
} else {
LinkedListNode current = head;//辅助指针
System.out.print("第" + (num + 1) + "条链表数据为 ");
while (true) {
System.out.print("id " + current.id + " 姓名 " + current.name);
if (current.next == null) {
break;
}
current = current.next;
}
System.out.println();
}
}

//查找数据
public void find(int id) {
if (head == null) {
System.out.println("没有此数据");
} else {
LinkedListNode current = head;//辅助指针
while (true) {
//已经遍历结束
if (current == null) {
break;
}
//已经找到
if (current.id == id) {
System.out.println("当前查找用户姓名为 " + current.name);
break;
}
current = current.next;
}
}
}

//修改数据
public void update(LinkedListNode node) {
if (head == null) {
System.out.println("请输入正确的用户id");
} else {
LinkedListNode current = head;//辅助指针
while (true) {
//已经遍历结束
if (current == null) {
System.out.println("请输入正确的用户id");
break;
}
//已经找到
if (current.id == node.id) {
current.name = node.name;
System.out.println("已成功将id为" + node.id + "的用户姓名修改为 " + node.name);
break;
}
current = current.next;
}
}
}

//删除数据
public void del(int id) {
if (head == null) {
System.out.println("请输入正确的用户id");
} else {
LinkedListNode current = head;//辅助指针
//因为此链表没有头节点,所有再定义一个辅助指针指向当前节点的前一个节点
LinkedListNode frontNode = new LinkedListNode(0, "");
frontNode.next = head;
while (true) {
//已经遍历结束
if (current == null) {
System.out.println("请输入正确的用户id");
break;
}
//已经找到
if (current.id == id) {
//如果需要删除的节点位于链表第一位,直接用下一个节点将其覆盖
if (frontNode.next == head) {
head = head.next;
} else {
frontNode.next = current.next;
}
break;
}
current = current.next;
frontNode = frontNode.next;
}
}
}
}

** 哈希表类
//哈希表类
public class HashTable {
LinkedList[] array;//存放数据得链表数组
int max;//数组最大长度

//初始化哈希表
public HashTable(int max) {
this.max = max;
array = new LinkedList[max];
//初始化每个数组
for (int i = 0; i < max; i++) {
array[i] = new LinkedList();
}
}

//散列函数,根据id再通过此函数得知需要操作哪条链表
private int hashNum(int id) {
return id % max;
}

//添加
public void add(LinkedListNode node) {
array[hashNum(node.id)].add(node);
}

//显示
public void show() {
for (int i = 0; i < max; i++) {
array[i].show(i);
}
}

//查找
public void find(int id) {
array[hashNum(id)].find(id);
}

//修改
public void update(LinkedListNode node) {
array[hashNum(node.id)].update(node);
}

//删除
public void del(int id) {
array[hashNum(id)].del(id);
}
}

** 测试类
//测试类
public class HashTableTest {
public static void main(String[] args) {
HashTable hashTable = new HashTable(7);
Scanner scanner = new Scanner(System.in);
String key;
while (true) {
System.out.println("add 添加数据");
System.out.println("show 展示数据");
System.out.println("find 展示数据");
System.out.println("update 修改数据");
System.out.println("del 删除数据");
System.out.println("exit 退出");
key = scanner.next();
switch (key) {
case "add":
System.out.println("输入id");
int id = scanner.nextInt();
System.out.println("输入姓名");
String name = scanner.next();
LinkedListNode node = new LinkedListNode(id, name);
hashTable.add(node);
break;
case "show":
hashTable.show();
break;
case "find":
System.out.println("输入需要查找的id");
hashTable.find(scanner.nextInt());
break;
case "update":
System.out.println("请输入需要修改用户的id");
int id2 = scanner.nextInt();
System.out.println("请输入需要新的用户名");
String name2 = scanner.next();
LinkedListNode node1 = new LinkedListNode(id2, name2);
hashTable.update(node1);
break;
case "del":
System.out.println("请输入需要删除的id");
hashTable.del(scanner.nextInt());
break;
case "exit":
scanner.close();
System.exit(0);
}
}
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: