数据结构与算法 12 哈希表 增删改查操作

哈希表(散列)

哈希表:不使用数据库;节省内存;速度快 — 存储、查找数据

根据键值码值直接进行访问的数据结构,通过把关键码值映射到表中一个位置来访问记录,以加快查找到速度。这个映射函数叫做散列函数,存放记录的数组叫做散列表


查找数据:

java 程序 — 缓存层(1. 缓存产品 redis、memcache;2.自己写:哈希表:数组+链表/数组+二叉树)— 数据库

将常用的数据从数据库加载到哈希表中,可以有多级缓存,提升查找效率


问题

要求:当新员工来报道,将其信息(id、年龄、性别、名字)加入,当输入该员工id时,能够查找到其所有信息

保证id从低到高添加


class Emp{id;name;address;} // 管理节点的信息
class EmpLinkedList{ // 管理链表的节点
	Emp head = null;
	// 相关的对雇员的操作
	// add
	// list
	// find
	// del
	// 散列函数 决定id对应哪个链表
}
class HashTable{ // 管理所有的链表
	EmpLinkedList[]
	empLikedListArr
	// 调用对链表的操作 增删改查
}
package hash;

import com.sun.org.apache.xalan.internal.xsltc.compiler.Template;

import java.util.Scanner;

public class HashTest {
    public static void main(String[] args) {
        // create a hash table
        HashTab hashTab = new HashTab(7);

        // menu
        String key = "";
        Scanner scanner = new Scanner(System.in);
        while(true){
            System.out.println("add: add emp");
            System.out.println("list: list emp");
            System.out.println("exit: exit system");
            System.out.println("find: find emp");
            System.out.println("delete: delete emp");
            key = scanner.next();
            switch (key){
                case "add":
                    System.out.println("input id");
                    int id = scanner.nextInt();
                    System.out.println("input name");
                    String name = scanner.next();
                    // create emp
                    Emp emp = new Emp(id, name);
                    hashTab.add(emp);
                    break;
                case "list":
                    hashTab.list();
                    break;
                case "find":
                    System.out.println("input id you want find");
                    id = scanner.nextInt();
                    hashTab.find(id);
                    break;
                case "delete":
                    System.out.println("input id you want to delete");
                    id = scanner.nextInt();
                    hashTab.delete(id);
                    break;
                case "exit":
                    scanner.close();
                    System.exit(0);
                    break;
                default:
                    break;
            }
        }
    }
}

// hash table
class HashTab{
    private EmpLinkedList[] empLinkedLists;
    private int size; // number of linkedlists
    // constructor
    public HashTab(int size){
        this.size = size;
        empLinkedLists = new EmpLinkedList[size];
        // initialize each linkedlist separately
        for (int i = 0; i < size; i++) {
            empLinkedLists[i] = new EmpLinkedList();
        }
    }
    // add emp
    public void add(Emp emp){
        // get emp's position according to emp's id
        int empLinkedListNo = hashFun(emp.id);
        // add emp to relative linkedlist
        empLinkedLists[empLinkedListNo].add(emp);
    }

    // traverse all linkedlists
    public void list(){
        for(int i = 0; i<size; i++){
            empLinkedLists[i].traverse(i);
        }
    }

    // find emp
    public void find(int id){
        // hash function
        int empLinkedListNum = hashFun(id);
        Emp emp = empLinkedLists[empLinkedListNum].findEmpById(id);
        if(emp!=null){
            System.out.printf("id %d is found it in %d\t",id,empLinkedListNum);
            System.out.printf("id is %d, name is %s",id,emp.name);
        }else{
            System.out.println("not find it ");
        }
    }

    public void delete(int id){
        int empLinkedListNum = hashFun(id);
        empLinkedLists[empLinkedListNum].delete(id);

    }


    // hash function, module
    public int hashFun(int id){
        return id % size;
    }
}


class Emp{
    public int id;
    public String name;
    public Emp next; // default is null
    public Emp(int id, String name){
        super();
        this.id = id;
        this.name = name;
    }
}

class EmpLinkedList{
    // head points to the first Emp, so this linkedlist head is meaningful
    private Emp head; // default is null

    // add Emp to linkedlist
    // 1. assume add id from small to big
    //    add emp to the last position of current linkedlist
    public void add(Emp emp){
        // if it is the first emp
        if(head == null){
            head = emp;
            return;
        }
        // if it is not the first emp, use assistant pointer
        Emp curEmp = head;
        while(true){
            if(curEmp.next==null){
                break;
            }
            curEmp = curEmp.next;
        }
        // add into linkedlist when out of the loop
        curEmp.next = emp;
    }

    // traverse linkedlist
    public void traverse(int num){
        if(head == null){
            System.out.println("list "+num+" is null");
            return;
        }
        Emp temp = head;
        while(true){
            System.out.printf("list " +num+ " => id = %d name = %s\t",temp.id,temp.name);
            if(temp.next==null){
                break;
            }
            temp = temp.next;
        }
        System.out.println();
    }

    // find emp
    public Emp findEmpById(int id){
        if(head==null){
            System.out.println("list is null");
            return null;
        }
        Emp curEmp = head;
        while(true){
            if(curEmp.id == id){
                break; // find it!
            }
            if(curEmp.next == null){
                // not find it
                curEmp = null;
                break;
            }
            curEmp = curEmp.next;
        }
        return curEmp;
    }

    public void delete(int id){
        if(head == null){
            System.out.println("list is null");
            return;
        }

        Emp curEmp = head;
        if(curEmp.id == id){
            curEmp = curEmp.next;
        }
        boolean flag = false;
        while(true){
            if(curEmp.next==null){
                break;
            }
            if(curEmp.next.id==id){
                flag = true;
                break;
            }
            curEmp = curEmp.next;
        }
        if(flag){
            curEmp.next = curEmp.next.next;
        }else{
            System.out.println("not fount it");
        }

    }
}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值