算法導論14-除法散列法_某年某月_新浪博客

算法導論14-除法散列法

        private void btn散列表_Click(object sender, EventArgs e)
        {
            int[] a = get_intArray(100);
            a = sjpxsz(a);
            var b = a.Take(20).ToArray();
            clsHash ch = new clsHash();
            string s="";
            try
            {
                for (int i = 0; i < b.Length; i++)
                {
                    var k = toolHX.cf_slb(b[i], 7);
                    var val = b[i];
                    clsDL d = new clsDL();
                    if (ch[k] != null)
                    {
                        d = (clsDL)ch[k];
                        if (d.head.val==k)
                        {
                            clsDL_dtl dl = new clsDL_dtl();
                            dl.key = val;
                            dl.val = k;
                            toolDL.dl_pop(d, dl);
                        }
                    }
                    else
                    {
                        d = new clsDL();
                        clsDL_dtl dl = new clsDL_dtl();
                        dl.key = val;
                        dl.val = k;
                        d.head = dl;
                        d.tail = dl;
                        d.ls.Add(dl);
                        ch[k] = d;
                    }
                }

                foreach (var k in ch.Keys)
                {
                    clsDL d = (clsDL)ch[k];
                    s += string.Format("樓層: {0} - 編號: ", d.head.val.ToString("00"));
                    var h = d.head;
                    while (h!=null)
                    {
                        s += string.Format("{0} ", h.key.ToString("00"));
                        h = h.next;
                    }
                    s += System.Environment.NewLine;
                }

            }
            catch (Exception ex)
            {
                s = ex.Message;
            }

            re_txt("除法散列法", s, 0);
        }

    #region 鏈接散列表

    public class clsHash : Hashtable
    {
        
    }

    public static class toolHX
    {
        public static clsDL_dtl hx_find(clsHash hx, int k1,int k2)
        {
            var x = hx[k1];
            if (x!=null)
            {
                var d = (clsDL)x;
                var l = toolDL.dl_search(d, k2);
                return l;
            }
            return null;
        }

        public static int cf_slb(int k,int m)
        {
            if (m>0)
            {
                return k % m;
            }
            return -1;
        }
    }

    #endregion


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值