C#数据结构与算法—哈希表

本文介绍了哈希表的基本原理和实现,包括哈希函数的设计以及添加、删除和查找操作。通过示例展示了哈希表在存储集合时的使用。同时提到了若关注元素顺序,红黑树是一个更好的选择。哈希表和红黑树在数据结构中的应用场景进行了对比。
摘要由CSDN通过智能技术生成
//测试哈希表
            int a = 123;
            Console.WriteLine(a.GetHashCode());
            int b = -123;
            Console.WriteLine(b.GetHashCode());
            float c = 0.5f;
            Console.WriteLine(c.GetHashCode());
            string d = "abc";
            Console.WriteLine(d.GetHashCode());
            Console.Read();

结果

123
-123
1056964608
536991770

在这里插入图片描述上述这些代码是将符号位屏蔽得到非负数的整数然后再取余

在这里插入图片描述 哈希表相关方法

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace DataStructure3
{
    class HashST1<Key>
    {
        private LinkedList1<Key>[] hashtable;
        private int N;
        private int M;
        public HashST1(int M)
        {
            this.M = M;
            N = 0;
            hashtable = new LinkedList1<Key>[M];
            for (int i = 0; i < M; i++)
            {
                hashtable[i] = new LinkedList1<Key>();
            }
        }
        public HashST1() : this(97) { }
        public int Count { get { return N; } }
        public bool IsEmpty { get { return N == 0; } }
        private int Hash(Key key)//这些代码是将符号位屏蔽得到非负数的整数然后再取余
        {
            return (key.GetHashCode() & 0x7fffffff) % M;
        }



        //添加元素方法
        public void Add(Key key)
        {
            LinkedList1<Key> list = hashtable[Hash(key)];
            if (list.Contains(key))
                return;
            else
                list.AddFirst(key);
            N++;
        }

        //删除方法
        public void Remove(Key key)
        {
            LinkedList1<Key> list = hashtable[Hash(key)];
            if (list.Contains(key))
            {
                list.Remove(key);
                N--;
            }

        }

        //是否包含
        public bool Contains(Key key)
        {
            LinkedList1<Key> list = hashtable[Hash(key)];
            return list.Contains(key);
        }
    }

}

若不在乎元素的顺序则使用哈希表,若在乎元素的顺序则使用红黑树

哈希表实现一个集合

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace DataStructure3
{
    class HashST1Set<Key>:ISet<Key>
    {
        private HashST1<Key> hashST1;

        public int Count => throw new NotImplementedException();

        public bool IsEmpty => throw new NotImplementedException();

        public HashST1Set(int M)
        {

            hashST1 = new HashST1<Key>(M);
        }
        public HashST1Set()
        {
            hashST1 = new HashST1<Key>();
        }

        public void Add(Key key)
        {
            hashST1.Add(key);
        }

        public void Remove(Key key)
        {
            hashST1.Remove(key);
        }

        public bool Contains(Key key)
        {
            return hashST1.Contains(key);
        }
    }
}

`

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值