Esempio n. 1
0
        public static void Main(string[] args)
        {
            HashTable hashtable = new HashTable(10);

            Console.WriteLine("Telephone area codes in the United States:");

            string[] states   = { "Arkansas", "California", "Colorado", "Florida", "Georgia", "Hawaii", "Iowa", "Illinois", "Indiana", "Kentucky", "Massachusetts", "Michigan", "Minnesota", "North Carolina", "New Jersey", "New York", "Ohio", "Oklahoma", "Pennsylvania", "Texas", "Virginia", "Washington" };
            string[] areaCode = { "479", "510", "303", "727", "478", "808", "641", "309", "574", "270", "978", "616", "763", "828", "973", "585", "330", "405", "215", "432", "804", "425" };

            for (int i = 0; i < states.Length; i++)
            {
                hashtable.Add(states[i], areaCode[i]);
            }

            Console.WriteLine("\nFind area codes for these states:");
            string[] getStates = { "Utah", "Florida", "New Jersey", "South Dakota", "Montana", "Missouri", "Kentucky", "Colorado", "California", "Alabama" };
            for (int i = 0; i < getStates.Length; i++)
            {
                hashtable.Get(getStates[i]);
            }

            Console.WriteLine("\nDoes the hashtable contains the area codes for these states?");
            string[] containsStates = { "Delaware", "Iowa", "Illinois", "Maine", "North Carolina" };
            for (int i = 0; i < containsStates.Length; i++)
            {
                Console.WriteLine($"Hashtable contains key \"{containsStates[i]}\" ? {hashtable.Contains(containsStates[i])}");
            }

            Console.WriteLine("\nHash these states:");
            string[] hashStates = { "Kansas", "Maryland", "Tennessee", "Oregon", "Alaska" };
            for (int i = 0; i < hashStates.Length; i++)
            {
                Console.WriteLine($"Hash \"{hashStates[i]}\" into bucket: {hashtable.Hash(hashStates[i])}");
            }
        }
Esempio n. 2
0
        static void Main(string[] args)
        {
            var charFinder    = new CharFinder();
            var nonRepeatChar = charFinder.FindFirstNonRepeatingCharacter("a apple product");
            var repeatChar    = charFinder.FindFirstRepeatedCharacter("a apple product");

            System.Console.WriteLine($"The first non repeat character is {nonRepeatChar}");
            System.Console.WriteLine($"The first repeated character is {repeatChar}");

            var hashTable = new HashTable(5);

            hashTable.Put(1, "A");
            hashTable.Put(2, "B");
            hashTable.Put(6, "C");
            System.Console.WriteLine($"Get value for key 6: {hashTable.Get(6)}");
            hashTable.Put(6, "D");
            System.Console.WriteLine($"Get value for key 6 after Update: {hashTable.Get(6)}");
            hashTable.Remove(6);
            System.Console.WriteLine($"Get value for key 6 after Remove: {hashTable.Get(6)}");
        }
        static void Main(string[] args)
        {
            Console.WriteLine("Hello World!");

            HashTable table = new HashTable(1024);

            table.Add("soup", "tomato");

            var value = table.Get("soup");

            Console.WriteLine(value);

            var exists  = table.Contains("soup");
            var exists2 = table.Contains("abc");

            System.Console.WriteLine($"Does it exist?  {exists}");
            System.Console.WriteLine($"Does it exist?  {exists2}");
        }