namespace HashtableClass
{
class Program
{
static void Main(string[] args)
{
string [] names = new string[99];
string name;
string[] someNames = new string[] {
"David","Jennifer","Donnie","Mayo","Raymond",
"Bernica","Mike","Clayton","Beata","Michael"};
int hashVal;
for (int i = 0; i < 10; i++)
{
name = someNames[i];
hashVal = SimpleHash(name, names);
names[hashVal] = name;
}
ShowDistrib(names);
}
static int SimpleHash(string s, string[] arr)
{
int tot = 0;
char[] cname;
cname = s.ToCharArray();
for (int i = 0; i < cname.GetUpperBound(0); i++)
{
tot += (int)cname[i];
}
return tot % arr.GetUpperBound(0);
}
static void ShowDistrib(string[] arr)
{
for (int i = 0; i < arr.GetUpperBound(0); i++)
{
if (arr[i] != null)
{
Console.WriteLine(i+" "+arr[i]);
}
}
}
}
}
************************this is a better function
namespace HashtableClass
{
class Program
{
static void Main(string[] args)
{
string [] names = new string[10007];//10007 is the best length for hash table
string name;
string[] someNames = new string[] {
"David","Jennifer","Donnie","Mayo","Raymond",
"Bernica","Mike","Clayton","Beata","Michael"};
int hashVal;
for (int i = 0; i < 10; i++)
{
name = someNames[i];
hashVal = SimpleHash(name, names);
names[hashVal] = name;
}
ShowDistrib(names);
Console.WriteLine("search 'David' with function InHash.....");
InHash("David", names);
Console.WriteLine("search 'Dav' with function InHash.....");
InHash("Dav", names);
}
static int SimpleHash(string s, string[] arr)
{
int tot = 0;
char[] cname;
cname = s.ToCharArray();
for (int i = 0; i < cname.GetUpperBound(0); i++)
{
tot += 37*tot + (int)cname[i];//***
}
tot = tot % arr.GetUpperBound(0);
if (tot < 0)//***
{
tot += arr.GetUpperBound(0);
}
return tot;
}
static void ShowDistrib(string[] arr)
{
for (int i = 0; i < arr.GetUpperBound(0); i++)
{
if (arr[i] != null)
{
Console.WriteLine(i+" "+arr[i]);
}
}
}
static void InHash(string s, string[] arr)
{
int hval = SimpleHash(s,arr);
if (arr[hval] == s)
{
Console.WriteLine("True");
}
else
{
Console.WriteLine("False");
}
}
}
}
******************************************
///
/// some time,a hash function returns the same value for two data items(collision),
/// one solution to the collision problem is to implement the hash table using buckets.
/// when using bucket hashing, the most important thing you can do is keep the number
/// of arraylist elements used as low as possible. this minimize the extra work that
/// has to be done when adding items to or removing items from the hash table.
///
public class BucketHash
{
private const int SIZE = 101;
ArrayList[] data;
public BucketHash()
{
data = new ArrayList[SIZE];
for (int i = 0; i < SIZE; i++)
{
data[i] = new ArrayList(4);
}
}
public int Hash(string s)
{
long tot = 0;
char[] charray;
charray = s.ToCharArray();
for (int i = 0; i < s.Length; i++)
{
tot += 37 * tot + (int)charray[i];
}
tot = tot % data.GetUpperBound(0);
if (tot < 0)
{
tot += data.GetUpperBound(0);
}
return (int)tot;
}
public void Insert(string item)
{
int hash_value;
hash_value = Hash(item);
if (data[hash_value].Contains(item))
{
data[hash_value].Add(item);
}
}
public void Remove(string item)
{
int hash_value;
hash_value = Hash(item);
if (data[hash_value].Contains(item))
{
data[hash_value].Remove(item);
}
}
}
*****************************************
//please import the System.Collections
static void Main(string[] args)
{
//begin to sue hashtable
Hashtable symbols = new Hashtable(25);
symbols.Add("salary", 100000);
symbols.Add("name","Mike");
symbols.Add("age", 45);
symbols.Add("dept", "Information Technology");
symbols["sex"] = "Male";
Console.WriteLine("this is a test about using hash table...");
Console.WriteLine();
foreach (object key in symbols.Keys)
{
Console.WriteLine(key.ToString()+" : "+symbols[key].ToString());
}
Console.WriteLine();
symbols.Remove("sex");
foreach (object key in symbols.Keys)
{
Console.WriteLine(key.ToString() + " : " + symbols[key].ToString());
}
Console.WriteLine();
Console.WriteLine("to retrieve the 'name'...");
if (symbols.ContainsKey("name"))
{
Console.WriteLine("found the name: " + symbols["name"].ToString());
}
else
{
Console.WriteLine("not exist...");
}
//end hashtable
}

