Exemple #1
0
 //test for basic functionality
 static public void TestAllPurposes(IHashTable <K, V> ht)
 {
     AddAll(ht, 50);
     ht.Print();
     Console.WriteLine("----------------------------------------");
     Remove(ht, (K)Convert.ChangeType("4", typeof(K)));
     ht.Print();
     Console.WriteLine("----------------------------------------");
     Add(ht, new HashNode <K, V>((K)Convert.ChangeType("4", typeof(K)), (V)Convert.ChangeType("104", typeof(V))));
     ht.Print();
     Console.WriteLine("----------------------------------------");
     Console.WriteLine(Search(ht, (K)Convert.ChangeType("4", typeof(K))));
     Console.WriteLine(Search(ht, (K)Convert.ChangeType("5", typeof(K))));
     Console.WriteLine("----------------------------------------");
     RemoveAll(ref ht, 50);
     ht.Print();
 }
Exemple #2
0
        /// <summary>
        /// Performs an action depending on the entered command.
        /// </summary>
        /// <param name="hashTable">Hash table to work with.</param>
        /// <param name="command">Command entered by user.</param>
        public void CommandExecution(IHashTable hashTable, int command)
        {
            switch (command)
            {
            case 0:
                break;

            case 1:
            {
                Console.WriteLine("Enter the data of element you want to add");
                var data = int.Parse(Console.ReadLine());

                if (!hashTable.Add(data))
                {
                    Console.WriteLine("The element with the given data already exists");
                }
                break;
            }

            case 2:
            {
                Console.WriteLine("Enter the data of element you want to delete");
                var data = int.Parse(Console.ReadLine());

                if (!hashTable.Delete(data))
                {
                    Console.WriteLine("The element with the given data does not exists");
                }
                break;
            }

            case 3:
            {
                Console.WriteLine("Enter the data of hash table element");
                var data = int.Parse(Console.ReadLine());

                if (hashTable.Exists(data))
                {
                    Console.WriteLine("The element with the given data exists");
                }
                else
                {
                    Console.WriteLine("The element with the given data does not exists");
                }
                break;
            }

            case 4:
                Console.WriteLine("The hash table:");
                hashTable.Print();
                break;

            default:
                Console.WriteLine("The command you entered is incorrect");
                break;
            }
        }
        private static void CommandExecution(IHashTable hashTable)
        {
            int number = int.Parse(Console.ReadLine());

            while (number != 0)
            {
                switch (number)
                {
                case 1:
                {
                    int value = ValueEntryRequest();
                    hashTable.Add(value);
                }
                break;

                case 2:
                {
                    int value = ValueEntryRequest();
                    hashTable.Remove(value);
                }
                break;

                case 3:
                {
                    int value = ValueEntryRequest();
                    if (hashTable.Exists(value))
                    {
                        Console.WriteLine("Value is in the hash table");
                    }
                    else
                    {
                        Console.WriteLine("Value is not in the hash table");
                    }
                }
                break;

                case 4:
                    hashTable.Print();
                    break;

                case 5:
                    hashTable.Clear();
                    break;

                default:
                    Console.WriteLine("Enter correct number");
                    break;
                }

                Console.Write("Input number: ");
                number = int.Parse(Console.ReadLine());
            }
        }