コード例 #1
0
        static void Main(string[] args)
        {
            Console.WriteLine("Hello World!");

            HashTable table = new HashTable();

            table.Add("tree", 10);
            int index = table.GetHash("tree");
            int value = table.Find("tree");

            Console.WriteLine($"The index of tree is: {index}");
            Console.WriteLine($"The value of tree is: {value}");
            Console.WriteLine();

            table.Add("sref", 15);
            int index2 = table.GetHash("sref");
            int value2 = table.Find("sref");

            Console.WriteLine($"The index of sref is: {index2}");
            Console.WriteLine($"The value of sref is: {value2}");
            Console.WriteLine();

            table.Add("cat", 25);
            int index3 = table.GetHash("cat");
            int value3 = table.Find("cat");

            Console.WriteLine($"The index of cat is: {index3}");
            Console.WriteLine($"The value of cat is: {value3}");
            Console.WriteLine();
            Console.WriteLine($"Checking to see if tree exists: {table.Contains("tree")}");
            Console.WriteLine($"Checking to see if sref exists: {table.Contains("sref")}");
            Console.WriteLine($"Checking to see if cat exists: {table.Contains("cat")}");
            Console.WriteLine($"Checking to see if dog exists: {table.Contains("dog")}");
        }
コード例 #2
0
        static void Main(string[] args)
        {
            Console.WriteLine("Add");
            HashTable <string> hashTable = new HashTable <string>();

            hashTable.Add("1");
            hashTable.Add("101");
            hashTable.Add("5");
            hashTable.Add("1");
            hashTable.Add(null);
            Console.WriteLine(hashTable);

            Console.WriteLine("Contains");
            Console.WriteLine(hashTable.Contains("99"));

            Console.WriteLine("Remove");
            hashTable.Remove("7");
            hashTable.Remove(null);
            Console.WriteLine(hashTable);

            Console.WriteLine("CopyTo");
            string[] array = new string[hashTable.Count + 2];
            hashTable.CopyTo(array, 2);
            Console.WriteLine(string.Join(", ", array));

            Console.WriteLine("Clear");
            hashTable.Clear();
            Console.WriteLine(hashTable);
        }
コード例 #3
0
        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}");
        }
コード例 #4
0
        public static void CreateHashTable()
        {
            HashTable table = new HashTable();

            table.Add("brian", "love DSA");
            Console.WriteLine("Add: brian, love DSA");
            Console.WriteLine($"Found in Hash Table: {table.Contains("brian")}");

            table.Add("I am", "6'4");
            Console.WriteLine("Add: I am, 6'4");
            Console.WriteLine($"Found in Hash Table: {table.Contains("I am")}");

            table.Add("sonics", "2022 or bust");
            Console.WriteLine("Add: sonics, 2022 or bust");
            Console.WriteLine($"Found in Hash Table: {table.Contains("sonics")}");

            table.Add("we I turn this in", "i'll be above 90%!");
            Console.WriteLine("Add: we I turn this in, i'll be above 90%");
            Console.WriteLine($"Found in Hash Table: {table.Contains("we I turn this in")}");
        }
コード例 #5
0
        public static void Main(string[] args)
        {
            HashTable HT = new HashTable();

            HT.Add("food", "potato");
            Console.WriteLine("Add: food, potato");
            Console.WriteLine($"Found in Hash Table: {HT.Contains("food")}");

            HT.Add("rats", "cat");
            Console.WriteLine("Add: rats, cat");
            Console.WriteLine($"Found in Hash Table: {HT.Contains("rats")}");

            HT.Add("fish", "grouper");
            Console.WriteLine("Add: fish, grouper");
            Console.WriteLine($"Found in Hash Table: {HT.Contains("fish")}");

            HT.Add("car", "porshe");
            Console.WriteLine("Add: car, porshe");
            Console.WriteLine($"Found in Hash Table: {HT.Contains("car")}");

            HT.Add("occupation", "dentist");
            Console.WriteLine("Add: occupation, dentist");
            Console.WriteLine($"Found in Hash Table: {HT.Contains("occupation")}");

            HT.Add("star", "pulsar");
            Console.WriteLine("Add: star, pulsar");
            Console.WriteLine($"Found in Hash Table: {HT.Contains("star")}");
        }
コード例 #6
0
        static void Main(string[] args)
        {
            Console.WriteLine("Hello World!");

            HashTable hashTable = new HashTable();

            hashTable.Add("A", "Apple");
            Console.WriteLine("Adding key: A and value: Apple");
            Console.WriteLine($"In hash table: {hashTable.Contains("A")}");

            hashTable.Add("B", "Banana");
            Console.WriteLine("Adding key: B and value: Banana");
            Console.WriteLine($"In hash table: {hashTable.Contains("B")}");

            hashTable.Add("on", "fire");
            Console.WriteLine("Adding key: on and value: fire");
            Console.WriteLine($"In hash table: {hashTable.Contains("on")}");

            hashTable.Add("no", "space");
            Console.WriteLine("Adding key: no and value: space");
            Console.WriteLine($"In hash table: {hashTable.Contains("no")}");
        }
コード例 #7
0
        public static void HashTableMadness()
        {
            HashTable ht    = new HashTable(1024);
            Node      node  = ht.Add("sushi", 2);
            Node      node2 = ht.Add("demi", 13);
            Node      node3 = ht.Add("coffee", 19);
            Node      node4 = ht.Add("kamehameha", 324);


            Console.WriteLine("confirming it contains added nodes");
            Console.WriteLine(ht.Contains("sushi"));
            Console.WriteLine(ht.Contains("demi"));
            Console.WriteLine(ht.Contains("coffee"));
            Console.WriteLine(ht.Contains("kamehameha"));


            Console.WriteLine("Finding nodes");
            Console.WriteLine(ht.Find("sushi"));
            Console.WriteLine(ht.Find("demi"));
            Console.WriteLine(ht.Find("coffee"));
            Console.WriteLine(ht.Find("kamehameha"));

            Console.WriteLine("Forcing collisions");
            ht.Add("Cat", 4);
            ht.Add("Doe", 9);
            ht.Add("fat", 17);
            ht.Add("gas", 20);

            Console.WriteLine($"Cat has an ascii value of {ht.asciiValue("Cat")}");
            Console.WriteLine($"Doe has an ascii value of {ht.asciiValue("Doe")}");
            Console.WriteLine($"fat has an ascii value of {ht.asciiValue("fat")}");
            Console.WriteLine($"gas has an ascii value of {ht.asciiValue("gas")}");



            Console.ReadKey();
        }
コード例 #8
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])}");
            }
        }