Страница 1 из 1

Пожалуйста, помогите с хэш-таблицей на C#

Добавлено: 07 окт 2010, 23:36
ind
Всем привет! Возникла проблема при работе с хэш-таблицей. Не осуществляется правильно поиск. Похоже, хэш-таблица у меня, вообще, остается пустой. Элементы, видимо, в нее не добавляются. Ниже прикладываю код. Буду рад, если поможете, исправить ошибки. Спасибо.

Код: Выделить всё

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

namespace HashConsole
{
    public class Info
    {
        public string Login;
        public string Password;
    }

    public class Hash_Element
    {
        public Info Element;
        public bool used;
    }

    public class Hash_Table
    {
        public const int SizeTable = 100;
        public int Size;
        public Hash_Element[] H_Table;

        public Hash_Table()
        {
            H_Table = new Hash_Element[SizeTable];
            Size = 0;
            for (int i = 0; i < H_Table.Length; i++)
            {
                H_Table[i] = new Hash_Element();
                H_Table[i].Element = new Info();
                H_Table[i].used = false;
            }
        }

        public int HashKey(string login)
        {
            int Sum = 0;
            int rez;

            for (int i = 0; i < login.Length; i++)
            {
                byte kod = Encoding.GetEncoding(1251).GetBytes(new char[] { login[i] })[0];
                Sum = Sum + kod;
            }
            rez = Sum % SizeTable;
            return rez;
        }

        public bool HashSearch(string login, out Info element)
        {
            int i = HashKey(login);
            while ((H_Table[i].used) && (H_Table[i].Element.Login != login))
            {
                i = (i+1) % SizeTable;
            }
            if (H_Table[i].used)
            {
                element = H_Table[i].Element;
                return true;
            }
            else
            {
                element = null;
                return false;
            }
        }

        public void HashInsert(Info element)
        {
            int i;
            Info info = new Info();
            if (!((Size == SizeTable - 1) || (HashSearch(element.Login, out info))))
            {
                i = HashKey(element.Login);
                while (H_Table[i].used)
                {
                    i = (i+1) % SizeTable;
                    H_Table[i].used = true;
                    H_Table[i].Element = element;
                    Size++;
                }
            }
        }
    }

    public class Program
    {
        static void Main()
        {
            Hash_Table H = new Hash_Table();
            string log, pas;
            log = "irina";
            pas = "09051945";
            Console.WriteLine("{0} {1}", log, pas);
            Info el = new Info();
            el.Login = log;
            el.Password = pas;
            H.HashInsert(el);
            log = "olga";
            pas = "08011989";
            Console.WriteLine("{0} {1}", log, pas);
            el.Login = log;
            el.Password = pas;
            H.HashInsert(el);
            log = "murka";
            pas = "123456";
            Console.WriteLine("{0} {1}", log, pas);
            el.Login = log;
            el.Password = pas;
            H.HashInsert(el);
            Console.ReadLine();
            Info info=new Info();
            if (H.HashSearch("irina", out info))
            {
                Console.WriteLine("Yes");
            }
            else
            {
                Console.WriteLine("No");
            }
            Console.ReadLine();
        }
    }
}
Все, помощь уже не нужна. Всем спасибо.