예제 #1
0
        /// <summary>
        /// Adds key into table
        /// </summary>
        /// <param name="key"></param>
        /// <param name="value"></param>
        public void Add(int key)
        {
            if (Count == Size)
            {
                return;
            }
            int hash = GetHash(key);

            if (Table[hash] == null)
            {
                Table[hash] = new HashItem(key);
                return;
            }

            if (Table[hash] != null)
            {
                Collisions++;
            }

            while (Table[hash] != null)
            {
                Table[hash] = Table[Rehash(hash)];
                hash        = Rehash(hash);
            }

            Table[hash] = new HashItem(key);
        }
예제 #2
0
 /// <summary>
 /// Prints table in console
 /// </summary>
 public void PrintTable()
 {
     for (int i = 0; i < Size; i++)
     {
         Console.Write("index: " + i + " key: ");
         HashItem current = Table[i];
         if (Table[i] != null)
         {
             Console.Write(Table[i].Key);
         }
         Console.WriteLine();
     }
 }
예제 #3
0
        private void ArrRenew()
        {
            HashTable        table = new HashTable(PriceArr.Length * 2);
            Queue <HashItem> queue = new Queue <HashItem>();

            for (int i = 0; i < PriceArr.Length; i++)
            {
                HashItem temp = PriceArr[0];
                while (temp != null)
                {
                    table.Add(temp.Name, temp.Price);
                    temp = temp.nextItem;
                }
            }
            PriceArr = table.PriceArr;
        }
예제 #4
0
        public void Add(string s, int price)
        {
            int n = HashFunc(s);

            if (PriceArr[n] == null)
            {
                PriceArr[n] = new HashItem(s, price);
            }
            else
            {
                HashItem temp = PriceArr[n];
                do
                {
                    if (temp.Name == s)
                    {
                        temp.Price = price;
                        return;
                    }
                    temp = temp.nextItem;
                }while (temp?.nextItem != null);
                temp.nextItem = new HashItem(s, price);
            }
        }