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

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

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

            Table[hash].Next = new MyHashTableItem(key);
        }
예제 #2
0
        /// <summary>
        /// Counts collision inside whole table
        /// </summary>
        /// <returns>Count of collisions</returns>
        public int CountCollisions()
        {
            int collisions = 0;

            for (int i = 0; i < Size; i++)
            {
                MyHashTableItem current = Table[i];
                if (current == null || current.Next == null)
                {
                    continue;
                }
                while (current.Next != null)
                {
                    collisions++;
                    current = current.Next;
                }
            }
            return(collisions);
        }
예제 #3
0
 /// <summary>
 /// Prints table in console
 /// </summary>
 public void PrintTable()
 {
     for (int i = 0; i < Size; i++)
     {
         Console.Write("[" + i + "]: {");
         MyHashTableItem current = Table[i];
         while (current != null)
         {
             Console.Write(Table[i].Key);
             if (current.Next != null)
             {
                 Console.Write(", ");
             }
             current = current.Next;
         }
         Console.Write("}");
         Console.WriteLine();
     }
 }