![算法導論14-除法散列法 算法導論14-除法散列法](https://i-blog.csdnimg.cn/blog_migrate/3e857a7654241ccd5f5b9040b0302243.jpeg)
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