C#实现哈希表

哈希表

介绍

散列表(Hash Table,也叫哈希表),是根据关键码值(key value)而直接进行访问的数据结构。也就是说,它通过把关键码值映射到表中的一个位置来访问记录,从而加快查找的速度。这个映射函数叫做散列函数,存放记录的数组叫做散列表。

实际需求

有一个公司,当有新的员工报到时,要求将该员工的信息加入(ID,姓名…),当输入该员工的 ID 时,要求查找到该员工的所有信息。

代码

using System;

namespace HashTableDemo
{
    class Program
    {
        static void Main(string[] args)
        {
            //创建一个哈希表
            HashTable hashTable = new HashTable(7);

            //测试
            string key = "";
            while (true)
            {
                Console.WriteLine("add: 添加雇员");
                Console.WriteLine("list: 显示雇员");
                Console.WriteLine("find: 查找雇员");
                Console.WriteLine("exit: 退出");

                key = Console.ReadLine();
                switch (key)
                {
                    case "add":
                        Console.WriteLine("输入id");
                        int id = Convert.ToInt32(Console.ReadLine());
                        Console.WriteLine("输入姓名");
                        string name = Console.ReadLine();

                        //创建雇员
                        Employee employee = new Employee(id, name);
                        hashTable.Add(employee);
                        break;
                    case "list":
                        hashTable.Traverse();
                        break;
                    case "find":
                        Console.WriteLine("输入带查找的雇员id");
                        id = Convert.ToInt32(Console.ReadLine());
                        hashTable.FindEmployeeByID(id);
                        break;
                    case "exit":
                        break;
                    default:
                        break;
                }
            }
        }
    }

    //创建 Hash Table
    class HashTable
    {
        private EmployeeLinkedList[] empLinkedListArray;
        private int size;   //表示共有多少条链表

        public HashTable(int size)
        {
            this.size = size;
            empLinkedListArray = new EmployeeLinkedList[size];

            //此处不要忘了分别初始化数组内部的每个链表
            for (int i = 0; i < size; i++)
            {
                empLinkedListArray[i] = new EmployeeLinkedList();
            }
        }

        public void Add(Employee employee)
        {
            //根据雇员 ID 得到该员工应该加入到哪条链表
            int empLinkedListNum = GetHashCode(employee.ID);

            //将employee  添加到对应的链表中
            empLinkedListArray[empLinkedListNum].Add(employee);
        }

        //遍历哈希表
        public void Traverse()
        {
            for (int i = 0; i < size; i++)
            {
                empLinkedListArray[i].Traverse(i);
            }
        }

        //根据输入的id查找雇员
        public void FindEmployeeByID(int id)
        {
            //使用散列函数确定查找哪条链表
            int empLinkedListNum = GetHashCode(id);

            Employee emp = empLinkedListArray[empLinkedListNum].FindEmployeeByID(id);
            if (emp != null)
            {
                Console.WriteLine($"在第{empLinkedListNum}条链表中找到该雇员 id = [{emp.ID}] name = [{emp.Name}]");
            }
            else
            {
                Console.WriteLine("在哈希表中未找到该雇员");
            }

        }

        //散列函数  使用简单的取模法
        public int GetHashCode(int id)
        {
            return id % size;
        }
    }

    //表示一个雇员
    class Employee
    {
        public int ID { get; set; }
        public string Name { get; set; }
        public Employee Next { get; set; }

        public Employee(int id, string name)
        {
            this.ID = id;
            this.Name = name;
            //this.Next = null;
        }
    }

    //表示链表
    class EmployeeLinkedList
    {
        //头指针,指向第一个Emp,因此该链表的head是有效的,直接指向第一个Emp
        private Employee head;  //默认为空

        //添加雇员到链表,直接加到链表末尾
        public void Add(Employee emp)
        {
            //如果是添加第一个雇员
            if (head == null)
            {
                head = emp;
                return;
            }

            //如果不是第一个雇员,则使用一个辅助指针定位到最后
            Employee curEmp = head;
            while (curEmp.Next != null)
            {
                curEmp = curEmp.Next;
            }

            //将emp 加入链表
            curEmp.Next = emp;
        }

        //遍历链表信息
        public void Traverse(int no)
        {
            if (head == null)
            {
                Console.WriteLine($"第{no}条链表为空!");
                return;
            }

            Console.Write($"第{no}条链表信息为:");

            Employee curEmp = head;
            while (curEmp != null)
            {
                Console.WriteLine($"=> id = [{curEmp.ID}]  name = [{curEmp.Name}]");
                curEmp = curEmp.Next;
            }
            return;
        }

        //根据id查找雇员,如果查找到,返回对应节点, 未找到返回空
        public Employee FindEmployeeByID(int id)
        {
            if (head == null)
                return null;

            Employee curEmp = head;
            while (curEmp != null)
            {
                if (curEmp.ID == id)
                    return curEmp;
                curEmp = curEmp.Next;
            }

            return null;
        }

    }
}

补充

在本例中,其实就是通过一个链表数组,将存储数据的链表拆分成几个小的链表,再通过对应的散列函数找到存储关键值对应的"小链表",从"小链表"中查找数据比在一个大的链表中查找数据更快。

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值